horizontal stack #33
Replies: 4 comments 7 replies
-
Formattable strings think you want to specify the kind of type going in the interpolation $"""
.rule {{
v---- this is why it didn't work with just %
width: %f{amount}%%
^--- youb basically need to escape it
}}
""" |
Beta Was this translation helpful? Give feedback.
-
I think you have arrived at the point where shadow DOM styling will start making sense, this is an example on how you would normally do it via CSS Custom Properties and Shadow DOM [<ImportMember("lit/directives/style-map.js")>]
let styleMap: obj -> TemplateResult = jsNative
[<LitElement("horiz-stack")>]
let HorizStack() =
let _, props =
LitElement.init(fun init ->
init.useShadowDom <- true
init.props <-
{|
columns = Prop.Of(defaultValue = 2, attribute = "columns")
gap = Prop.Of(defaultValue = "0px", attribute = "gap")
|}
// This is the place where you can put any shared styles
// These styles are applied via Shadow DOM and are "Scoped" to this element
// the benefit is that you styles get declared once per component definition
// and not per component instance (like in the example you could figure out)
init.styles <- [
css
$"""
.grid {{
display: grid;
grid-template-columns: repeat(var(--column-count, 1), 1fr);
grid-gap: var(--column-gap, 8px);
}}
::slotted(div) {{
box-sizing: border-box;
}}
"""
]
)
// here we leverage CSS Custom Properties
// to update them via our properties for this element
// but they could also be modified by the user's stylesheet
// to have a different set of defaults (4 columns rather than 1, 1em gap rather than in px)
let styles =
{| ``--column-count`` = props.columns.Value
``--column-gap`` = props.gap.Value |}
html $"""
<!-- we apply our css variable overides -->
<div class="grid" style="{styleMap styles}">
<slot></slot>
</div>
""" let view() =
html
$"""
<main>
<horiz-stack gap="30px" .columns={3}>
<div>1</div>
<div>2</div>
<div>3</div>
</horiz-stack>
{match state with
| Page.Home -> Home.View()
| Page.About -> About.View()}
</main>""" |
Beta Was this translation helpful? Give feedback.
-
The custom css properties looks useful. Thanks for pointing that out. Is there a difference between setting |
Beta Was this translation helpful? Give feedback.
-
I'm just now implementing your suggestions about with the custom css properties and One issue I am having though... Is it possible to make it so that I can added a For example, this style will not be merged and will be ignored: <horiz-stack .columns={2} style="background: red;">
<div></div>
<div></div>
</horiz-stack> |
Beta Was this translation helpful? Give feedback.
-
I got it working!
Usage:
The problem I had before was that I was trying to use
grid-template-columns: 50% 50%
, which worked fine in my css file, but glitched out when I put it inside thehtml $""" """
. Something about the percent sign made the formatting look weird and then it just wouldn't work. Changing it to use1fr
gets around this problem, and is a nicer way of doing it anyway.Beta Was this translation helpful? Give feedback.
All reactions