styled-jsx-dynamic
npx machina-cli add skill andireuter/skills-webapp/styled-jsx-dynamic --openclawFiles (1)
SKILL.md
1.2 KB
styled-jsx Dynamic Styling Skill
Implement dynamic styles without sacrificing maintainability or safety.
Prefer this order
- CSS variables set via style prop
- style={{ "--accent": color } as React.CSSProperties }
- className toggling
- data-state, data-variant
- Interpolated template literals inside <style jsx> only when needed
- Keep interpolations small, numeric, or enumerated
- Never interpolate untrusted strings into selectors or urls
Output expectations
- Update the component.
- Provide an explanation of why the chosen strategy is best.
- Include minimal test coverage for the dynamic behavior.
Example patterns
-
CSS vars:
<div style={{ "--gap": `${gap}px` } as React.CSSProperties } /> <style jsx>{`div{ gap: var(--gap, 8px); }`}</style> -
class toggling:
<div data-open={open ? "true" : "false"} /> <style jsx>{`div[data-open="true"]{ ... }`}</style>
Source
git clone https://github.com/andireuter/skills-webapp/blob/main/skills/styled-jsx-dynamic/SKILL.mdView on GitHub Overview
Learn to implement dynamic styles in styled-jsx while keeping maintainability and safety. This approach prioritizes CSS variables set via the style prop, then class-like toggling with data attributes, and uses interpolations inside style jsx only when necessary and safe.
How This Skill Works
Set CSS variables using the component’s style prop and reference them in a style jsx block. Use data-state or data-variant attributes to drive conditional styling, providing a clean alternative to complex CSS-in-JS logic. Keep interpolations inside style jsx small and safe; never inject untrusted strings into selectors or URLs.
When to Use It
- Theming across a component or app to swap palettes at runtime
- Hover, focus, and active state styling for interactive elements
- Variant switching (size, density, or style variants) via data-variant
- Runtime changes to size or color controlled by props
- Safe use of interpolations to adjust values with constraints
Quick Start
- Step 1: Identify the dynamic styling need and prefer CSS vars via the style prop
- Step 2: Apply CSS variables through the style prop to pass runtime values
- Step 3: Add a style jsx block that uses the variables and data-attributes for toggling; keep interpolations safe
Best Practices
- Prefer CSS vars set via the style prop to drive dynamic values
- Use data-state or data-variant attributes for class toggling
- Keep interpolations inside style jsx small, numeric, or enumerated
- Never interpolate untrusted strings into selectors or URLs
- Include minimal test coverage for dynamic behavior
Example Use Cases
- Themed Button that uses a runtime color variable to style hover
- Variant Card that switches between elevated and flat styles using data-variant
- Interactive Toggle where data-open controls open/closed styles
- Resizable Badge that adjusts padding and font with a CSS var
- Safe interpolation example showing a small numeric value for spacing
Frequently Asked Questions
Add this skill to your agents