sdxc

[ @sdxc/u/general ]

var

Resolves a custom property reference: var(--{name}), or with a fallback, var(--{name}, {fallback}).

import { var } from "@sdxc/u/general";

Quick reference

Every documented call, beside the CSS it emits.

CallCSS
u.var("sidebar-width")"var(--sidebar-width)"
u.var("sidebar-width", "18rem")"var(--sidebar-width, 18rem)"

Examples

Basic example

Use u.var(name, fallback) to read a custom property in any utility that takes a CSS value; it is written u.var because var is a reserved word:

main
<div mix={[hstack({ gap: 4 })]}>
	<aside mix={[is(u.var("sidebar-width", "12rem")), ...]}>sidebar</aside>
	<main mix={[grow(), ...]}>main</main>
</div>

Setting it from a parent

Set the property on an ancestor with u.vars() and every u.var() below it picks the value up, so one layout adapts by context:

main
main
<div mix={[...]}>
	<div mix={[hstack({ gap: 4 })]}>
		<aside mix={[is(u.var("sidebar-width", "12rem")), ...]}>12rem</aside>
		<main mix={[grow(), ...]}>main</main>
	</div>
	<div mix={[u.vars({ "sidebar-width": "20rem" }), hstack({ gap: 4 })]}>
		<aside mix={[is(u.var("sidebar-width", "12rem")), ...]}>20rem</aside>
		<main mix={[grow(), ...]}>main</main>
	</div>
</div>

Applying on a state

Wrap the call in a state utility to scope it to one selector.

u.hover(u.var("sidebar-width"))

Responsive design

Wrap the call in a container query to scope it to one width.

u.at("md", u.var("sidebar-width"))

Signature

u.var(name: string, fallback?: string)

Written by Sergio Xalambrí. Follow @sergiodxa for new packages, or sponsor the work.