I’m wondering how I can translate this js example into liveview:
(I’ve simplified the example for the question)
import clsx from 'clsx'
const styles = {
note: {
container:
'bg-sky-50 dark:bg-slate-800/60 dark:ring-1 dark:ring-slate-300/10',
},
warning: {
container:
'bg-amber-50 dark:bg-slate-800/60 dark:ring-1 dark:ring-slate-300/10',
},
}
export function Callout({ type = 'note', children }) {
return (
<div className={clsx('my-8 flex rounded-3xl p-6', styles[type].container)}>
{children}
</div>
)
}
- Having a variable
styles
doesn’t work (liveview doesn’t like variables - Assigns and HEEx templates — Phoenix LiveView v0.18.3). - Putting the styles into assigns on every render feels like overkill.
- Having a constant
@styles
defined at the top of the module doesn’t work. Because then the template searches for@styles
in assigns instead of the constant.
The only option I’ve found so far is something like this:
defp callout(assigns) do
~H"""
<div class={["my-8 flex rounded-3xl p-6", @type == "note" && "", @type == "warning" && ""]}>
</div>
"""
end
This gets ugly fast as there are more types added and there are more styles (for a title, children,…)
Is there anything I’m missing?