@event
Tutorial
The @event
function in FSCSS lets you add logic
and dynamic behavior into your stylesheets. Instead of repeating values,
you can create reusable functions that return different results based on conditions.
@event
, your CSS can decide what value to use
at runtime — just like functions in programming.
To create an event, use the @event
keyword, give it a
name, and optionally pass parameters. Inside, write conditions that return values.
/* Basic event */ @event theme(mode) { if mode: dark { return: #111111; } el { return: #ffffff; } } body { background: @event.theme(dark); color: @event.theme(light); }
Inside events, you can use if
, el-if
, and el
blocks:
if
→ Primary checkel-if
→ Additional checksel
→ Fallback if no condition matched@event size(type) { if type: small { return: 12px; } el-if type: medium { return: 16px; } el { return: 20px; } } p { font-size: @event.size(small); }
FSCSS events support operators for number comparisons:
==
→ Equal>
→ Greater than<
→ Less than>=
→ Greater than or equal<=
→ Less than or equal@event rating(score) { if score >= 90 { return: green; } el-if score >= 50 { return: orange; } el { return: red; } } .user-score { color: @event.rating(72); }
Events can also use num()
for calculations and @random()
for randomness.
/* Random colors */ str(primary, "@random([#ff0000, #00ff00, #0000ff])") /* Spacing calculator */ @event spacing(level) { if level == 1 { return: num(4*2)px; } el-if level == 2 { return: num(8*2)px; } el { return: num(16*2)px; } } .card { background: primary; padding: @event.spacing(2); }
Valuable events are reusable helpers that save time. For example, instead
of repeating breakpoints everywhere, create a @event
once.
@event device(width) { if width <= 480 { return: mobile; } el-if width <= 1024 { return: tablet; } el { return: desktop; } } .container { flex-direction: @event.device(768); }
Events can combine with variables for even more flexibility.
$second-test: #302030; @event themedBackground(mode) { if mode: dark { return: $second-test!; } el { return: ; } } body { background: @event.themedBackground(dark); }
spacing
, theme
, device
)str()
when possible/* */
), not //