DEEP DIVE

Compiled lookup

Look up a style expression and compare its source with the compiled output.

Plumeria resolves static styles to class names and CSS at build time. Runtime conditions select among those class names, and dynamic function key arguments supply CSS variable values. Each example uses these definitions.

import * as css from '@plumeria/core';
const s = css.create({
  base: { padding: 8 },
  red: { color: 'red' },
  blue: { color: 'blue' },
  w: (n) => ({ width: n }),
  tone: (c) => ({ color: c }),
});

Static styles and conditions

One static style

Input
export const A = () => <div sx={s.red} />;
Output
export const A = () => <div className="xq96bg3w" />;

An array of static styles

Input
export const A = () => <div sx={[s.base, s.red]} />;
Output
export const A = () => <div className="xymmdeuh xq96bg3w" />;

className before sx

Input
export const A = () => <div className="kept" sx={s.red} />;
Output
export const A = () => <div className="xq96bg3w" />;

sx before className

Input
export const A = () => <div sx={s.red} className="kept" />;
Output
export const A = () => <div className="kept" />;

A conditional style with &&

Input
export const A = ({ on }) => <div sx={[s.base, on && s.red]} />;
Output
export const A = ({ on }) => <div className={(on ? "xymmdeuh xq96bg3w" : "xymmdeuh")} />;

A ternary expression

Input
export const A = ({ on }) => <div sx={on ? s.red : s.blue} />;
Output
export const A = ({ on }) => <div className={(on ? "xq96bg3w" : "xgmn1kmt")} />;

Conditions that set the same property

Input
export const A = ({ a, b }) => <div sx={[a && s.red, b && s.blue]} />;
Output
export const A = ({ a, b }) => <div className={((b) ? "xgmn1kmt" : ((a) ? "xq96bg3w" : ""))} />;

A function call in a condition

Input
export const A = ({ f }) => <div sx={[s.base, f() && s.red]} />;
Output
export const A = ({ f }) => <div className={((f()) ? "xymmdeuh xq96bg3w" : "xymmdeuh")} />;

Selecting a style with s[v]

Input
export const A = ({ v }) => <div sx={s[v]} />;
Output
export const A = ({ v }) => <div className={({__proto__:null,"base":"xymmdeuh","red":"xq96bg3w","blue":"xgmn1kmt"}[v] || "")} />;

Function keys

A literal argument

Input
export const A = () => <div sx={s.w(10)} />;
Output
export const A = () => <div className="xl77b4e6" style={{ "--xkvwfgqc-n": "10px" }} />;

A dynamic argument

Input
export const A = ({ n }) => <div sx={s.w(n)} />;
Output
export const A = ({ n }) => <div className="xl77b4e6" style={{ "--xkvwfgqc-n": (typeof (n) === 'number' ? (n) + 'px' : (n)) }} />;

An expression as an argument

Input
export const A = ({ n }) => <div sx={s.w(n * 2)} />;
Output
export const A = ({ n }) => { let __plumeria__$$a0; return <div className="xl77b4e6" style={(__plumeria__$$a0 = n * 2, { "--xkvwfgqc-n": (typeof (__plumeria__$$a0) === 'number' ? (__plumeria__$$a0) + 'px' : (__plumeria__$$a0)) })} />; };

Combining a function key with a static style

Input
export const A = ({ n }) => <div sx={[s.base, s.w(n)]} />;
Output
export const A = ({ n }) => <div className="xymmdeuh xl77b4e6" style={{ "--xkvwfgqc-n": (typeof (n) === 'number' ? (n) + 'px' : (n)) }} />;

Combining a function key with a condition

Input
export const A = ({ n, on }) => <div sx={[s.w(n), on && s.red]} />;
Output
export const A = ({ n, on }) => <div className={(on ? "xl77b4e6 xq96bg3w" : "xl77b4e6")} style={{ "--xkvwfgqc-n": (typeof (n) === 'number' ? (n) + 'px' : (n)) }} />;

Calling a function key conditionally

Input
export const A = ({ n, on }) => <div sx={on && s.w(n)} />;
Output
export const A = ({ n, on }) => <div className={(on ? "xl77b4e6" : "")} style={on ? { "--xkvwfgqc-n": (typeof (n) === 'number' ? (n) + 'px' : (n)) } : undefined} />;

Switching function key arguments with a ternary

Input
export const A = ({ on, a, b }) => <div sx={on ? s.tone(a) : s.tone(b)} />;
Output
export const A = ({ on, a, b }) => <div className="x5fhmvjn" style={on ? { "--xefxcruu-c": (typeof (a) === 'number' ? (a) + 'px' : (a)) } : { "--xefxcruu-c": (typeof (b) === 'number' ? (b) + 'px' : (b)) }} />;

Two function keys

Input
export const A = ({ n, c }) => <div sx={[s.w(n), s.tone(c)]} />;
Output
export const A = ({ n, c }) => <div className="xl77b4e6 x5fhmvjn" style={{ "--xkvwfgqc-n": (typeof (n) === 'number' ? (n) + 'px' : (n)), "--xefxcruu-c": (typeof (c) === 'number' ? (c) + 'px' : (c)) }} />;

JSX attributes

An existing style object

Input
export const A = ({ n }) => <div style={{ top: 1 }} sx={s.w(n)} />;
Output
export const A = ({ n }) => <div style={{ top: 1 }} className="xl77b4e6" {...{ "style": { "--xkvwfgqc-n": (typeof (n) === 'number' ? (n) + 'px' : (n)) } }} />;

An existing style variable

Input
export const A = ({ n, st }) => <div sx={s.w(n)} style={st} />;
Output
export const A = ({ n, st }) => <div className="xl77b4e6" {...{ "style": { "--xkvwfgqc-n": (typeof (n) === 'number' ? (n) + 'px' : (n)) } }} style={st} />;

A className expression

Input
export const A = ({ n, cls }) => <div className={cls} sx={s.w(n)} />;
Output
export const A = ({ n, cls }) => <div className="xl77b4e6" style={{ "--xkvwfgqc-n": (typeof (n) === 'number' ? (n) + 'px' : (n)) }} />;

An attribute between sx and style

Input
export function A({ c, f, g }) {
  return <div sx={[s.tone(c), f() && s.red]} id={g()} style={f()} />;
}
Output
export function A({ c, f, g }) {
  return <div className={((f()) ? "xq96bg3w" : "x5fhmvjn")} {...{ "style": { "--xefxcruu-c": (typeof (c) === 'number' ? (c) + 'px' : (c)) } }} id={g()} style={f()} />;
}

A spread and className after sx

Input
export const A = ({ n, props }) => <div sx={s.w(n)} {...props} className="base" />;
Output
export const A = ({ n, props }) => <div {...{ "className": "xl77b4e6" }} style={{ "--xkvwfgqc-n": (typeof (n) === 'number' ? (n) + 'px' : (n)) }} {...props} className="base" />;

A spread after a static style

Input
export const A = (props) => <div sx={s.red} {...props} className="base" />;
Output
export const A = (props) => <div {...{ "className": "xq96bg3w" }} {...props} className="base" />;

Evaluation order and scope

Inside a function declaration

Input
export function A({ n, on }) {
  return <div sx={[on && s.red, s.w(n)]} />;
}
Output
export function A({ n, on }) {
  return <div className={(on ? "xl77b4e6 xq96bg3w" : "xl77b4e6")} style={{ "--xkvwfgqc-n": (typeof (n) === 'number' ? (n) + 'px' : (n)) }} />;
}

A function body with a directive

Input
export function A({ n }) {
  'use client';
  return <div sx={s.w(n)} />;
}
Output
export function A({ n }) {
  'use client';
  return <div className="xl77b4e6" style={{ "--xkvwfgqc-n": (typeof (n) === 'number' ? (n) + 'px' : (n)) }} />;
}

At module scope

Input
const on = true, n = 1;
export const el = <div sx={[on && s.red, s.w(n)]} />;
Output
const on = true, n = 1;
export const el = <div className={(on ? "xl77b4e6 xq96bg3w" : "xl77b4e6")} style={{ "--xkvwfgqc-n": (typeof (n) === 'number' ? (n) + 'px' : (n)) }} />;

Inside a default parameter

Input
export function P(x = <div sx={[mark() && s.red, s.w(n)]} />) { return x; }
Output
function __plumeria__$$f0(__plumeria__$$a0) { return ({ "--xkvwfgqc-n": (typeof (__plumeria__$$a0) === 'number' ? (__plumeria__$$a0) + 'px' : (__plumeria__$$a0)) }); }


export function P(x = <div className={((mark()) ? "xl77b4e6 xq96bg3w" : "xl77b4e6")} style={__plumeria__$$f0(n)} />) { return x; }

css.use and component props

A conditional argument to css.use

Input
export const cls = (on) => css.use(s.base, on && s.red);
Output
export const cls = (on) => (on ? "xymmdeuh xq96bg3w" : "xymmdeuh");

A function call in a css.use condition

Input
export const cls = (f) => css.use(f() && s.red, s.blue);
Output
export const cls = (f) => ((f()), "xgmn1kmt");

Passing styles to a component

Input
const Card = ({ cardStyle }) => <div sx={[s.base, cardStyle]} />;
export const A = ({ n, on }) => <Card cardStyle={[s.w(n), on && s.red]} />;
Output
const Card = ({ cardStyle }) => <div className={({__proto__:null,"xxsrw8jm":"xymmdeuh xl77b4e6 xq96bg3w","xqehy1ty":"xymmdeuh xl77b4e6"}[((cardStyle && cardStyle.key) || cardStyle)] || "xymmdeuh")} style={{ ...(cardStyle && cardStyle.vars) }} />;
export const A = ({ n, on }) => <Card cardStyle={((on) ? { key: "xxsrw8jm", vars: { "--xkvwfgqc-n": (typeof (n) === 'number' ? (n) + 'px' : (n)) } } : { key: "xqehy1ty", vars: { "--xkvwfgqc-n": (typeof (n) === 'number' ? (n) + 'px' : (n)) } })} />;
Edit on GitHub