truss
truss copied to clipboard
feat: Proof of concept non-component-based css prop.
This is a spike related to https://dev.to/srmagura/why-were-breaking-up-wiht-css-in-js-4g9b and specifically re "can we get rid of the wrapping EmotionCssPropInternal components" that currently are required anytime a css= prop is used.
See https://github.com/reactwg/react-18/discussions/110 but it would be great if React had a non-hook way of subscribing to useInsertionEffect events, like document.addEventElement('insertionEffect", () => ...).
Misc ideas:
- Just try stock fela &
FelaComponent, to see perf vs. emotion- Would still have wrapper components in the React tree, but just curious
- Call the fela
rendererdirectly inline in__jsxDEV(noFelaComponent) and :shrug: if the performance of "not usinguseInsertionEffectreally matters- I.e.
styles would be injected ~randomly after each render and not necessarily at "safe insertion points", which would cause more DOM/CSS recalcs per the React-18 link - Complain to react about the lack of a global
insertionEffectevent/hook
- I.e.
- Output any non-selector styles directly to the
styleattribute, and skip class names all together (recommended in the react-18 doc as what FB does, but technically only for dynamic styles)- Output any selector-based styles via Emotion/Fela as usual
- Use already-injected awareness to only conditionally wrap in a
FelaComponent, otherwise use the existing/injected classes directly onclassName(noFelaComponent)
Another idea for component-less usage:
export function FooComponent() {
const css = useTruss();
return <div className={css.mt2.mb3.$} />;
}
- Pro: "Fine, fine" uses a hook to get
css, which will play nicer with React 18'suseInsertionEffecthook- See the
useCSSexample from thereactwglink in the PR description - Granted, Emotion's
css=prop already does this for us: https://github.com/emotion-js/emotion/blob/acb72a45592881d9d1f72003b6db2e488b981599/packages/react/src/emotion-element.js#L61
- See the
- Con: Burns a LOC in basically every component
- Pro: Uses just
classNameso doesn't need thecss=...JSX wrapper- This removes the
jsxImportSource/jsxFactorybuild config from vite/storybook/etc (which we have working now, but can be flakey when upgrading/changing configs) - This also removes the
EmotionalInternalCsswrapper component that Emotion'scss=prop is forced to inject for it to add its own hooks to resolve emotion from a context
- This removes the