style-zx 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,146 @@
1
+ # Style-ZX
2
+
3
+ **Zero-runtime CSS-in-JS with a `zx` prop.**
4
+
5
+ `style-zx` is a lightweight, zero-runtime CSS-in-JS library designed for Vite. It allows you to style your React components using a `zx` prop, which is compiled to static CSS classes at build time. This combines the developer experience of CSS-in-JS with the performance of static CSS.
6
+
7
+ ## Features
8
+
9
+ - **Zero Runtime**: Styles are extracted to static CSS files during the build process. No runtime style injection or overhead.
10
+ - **`zx` Prop**: Style any component directly with the `zx` prop (inspired by MUI's `sx` and other similar libraries).
11
+ - **TypeScript Support**: Full type safety for CSS properties and theme variables.
12
+ - **Theming**: Define a theme and access variables easily (e.g., `"$theme.colors.primary"`).
13
+ - **Aliases**: Shorthand properties for common styles (e.g., `p`, `m`, `px`, `my`, `bg`).
14
+ - **Nested Selectors**: Support for pseudo-classes and nested selectors (e.g., `&:hover`, `& > div`).
15
+ - **Vite Integration**: Seamless integration as a Vite plugin with HMR support.
16
+
17
+ ## Installation
18
+
19
+ 1. **Install the package** (assuming local or published package):
20
+
21
+ ```bash
22
+ npm install style-zx
23
+ # or
24
+ yarn add style-zx
25
+ ```
26
+
27
+ 2. **Add the Vite plugin** in `vite.config.ts`:
28
+
29
+ ```typescript
30
+ import { defineConfig } from 'vite'
31
+ import react from '@vitejs/plugin-react'
32
+ import styleZx from 'style-zx/vite-plugin' // Adjust path if local
33
+
34
+ export default defineConfig({
35
+ plugins: [react(), styleZx()],
36
+ })
37
+ ```
38
+
39
+ ## Usage
40
+
41
+ ### Basic Styling
42
+
43
+ Use the `zx` prop on any HTML element. Numeric values for dimensions are treated as pixels by default.
44
+
45
+ ```tsx
46
+ <div zx={{
47
+ bg: 'white',
48
+ p: 20, // padding: 20px
49
+ borderRadius: 8,
50
+ boxShadow: '0 4px 6px rgba(0,0,0,0.1)'
51
+ }}>
52
+ <h1 zx={{ color: 'blue', fontSize: 24 }}>Hello World</h1>
53
+ </div>
54
+ ```
55
+
56
+ ### Theming
57
+
58
+ 1. **Define your theme** and create the hook:
59
+
60
+ ```typescript
61
+ // src/style-zx/theme.ts
62
+ import { createTheme } from 'style-zx';
63
+
64
+ export const { useTheme, theme } = createTheme({
65
+ colors: {
66
+ primary: '#007bff',
67
+ background: '#f0f2f5',
68
+ text: '#333'
69
+ },
70
+ spacing: {
71
+ small: 8,
72
+ medium: 16
73
+ }
74
+ });
75
+ ```
76
+
77
+ 2. **Use theme variables** in your components. You can reference them as strings starting with `$theme.`.
78
+
79
+ ```tsx
80
+ import { useTheme } from './style-zx';
81
+
82
+ function App() {
83
+ const theme = useTheme();
84
+
85
+ return (
86
+ <button zx={{
87
+ bg: '$theme.colors.primary',
88
+ color: 'white',
89
+ p: '$theme.spacing.small'
90
+ }}>
91
+ Click Me
92
+ </button>
93
+ )
94
+ }
95
+ ```
96
+
97
+ ### Nested Selectors
98
+
99
+ You can use standard CSS nesting syntax.
100
+
101
+ ```tsx
102
+ <div zx={{
103
+ color: 'black',
104
+ '&:hover': {
105
+ color: 'blue'
106
+ },
107
+ '& > span': {
108
+ fontWeight: 'bold'
109
+ }
110
+ }}>
111
+ Hover me <span>(Bold)</span>
112
+ </div>
113
+ ```
114
+
115
+ ## Comparison & Concept
116
+
117
+ ### The Concept
118
+ `style-zx` relies on **static analysis**. The build plugin scans your code for the `zx` prop, extracts the object literal, generates a unique class hash, creates CSS rules, and replaces the `zx` prop with a `className`.
119
+
120
+ ### vs. Pigment CSS
121
+ Both libraries aim for zero-runtime CSS-in-JS.
122
+ - **Pigment CSS**: A more robust, complex solution often integrated with Next.js and MUI's ecosystem. It handles more complex dynamic scenarios but requires deeper integration. Also the project is not actively maintained at the moment.
123
+ - **Style-ZX**: A lightweight, Vite-first approach. It focuses on simplicity and the specific `zx` prop API. It's easier to set up for simple Vite projects but may have fewer features than Pigment.
124
+
125
+ ### vs. Emotion / Styled-Components
126
+ - **Emotion/Styled-Components**: Runtime CSS-in-JS. They parse styles in the browser, generate classes, and inject tags. This offers great flexibility (dynamic props) but incurs a runtime performance cost (script execution + style recalculation).
127
+ - **Style-ZX**: No runtime cost. The browser just loads a CSS file.
128
+
129
+ ### vs. Tailwind CSS
130
+ - **Tailwind**: Utility-first. You compose classes (`p-4 bg-white`).
131
+ - **Style-ZX**: Object-based. You write CSS-like objects (`{ p: 16, bg: 'white' }`). This is often preferred by developers who like keeping styles colocated but find long class strings hard to read.
132
+
133
+ ## Caveats & Limitations
134
+
135
+ 1. **Static Analysis Only**: The values in `zx` must be statically analyzable at build time.
136
+ - ✅ `zx={{ color: 'red' }}`
137
+ - ✅ `zx={{ color: '$theme.colors.primary' }}` (if theme is static)
138
+ - ❌ `zx={{ color: props.color }}` (Dynamic props are **not** supported directly in the build step. Use CSS variables for dynamic values).
139
+ 2. **Vite Only**: Currently designed specifically as a Vite plugin.
140
+ 3. **No Dynamic Function Interpolations**: You cannot pass a function to `zx` that depends on runtime state.
141
+
142
+ ## Gains
143
+
144
+ - **Performance**: Zero JS runtime for styles means faster TTI (Time to Interactive) and less main-thread work.
145
+ - **Developer Experience**: Write styles in TypeScript right next to your components. Get autocomplete and type checking.
146
+ - **Maintainability**: Styles are scoped and colocated, reducing dead code and global namespace pollution.
package/dist/index.cjs ADDED
@@ -0,0 +1,3 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("react");function f(n,c="--theme"){const e={};function t(s,r){for(const u in s){const o=s[u],m=r?`${r}-${u}`:u;typeof o=="object"&&o!==null?t(o,m):e[`${c}-${m}`]=o}}return t(n,""),e}let l={};const i=new Set;function d(n){l=n;const c=f(n);let e=document.getElementById("style-zx-theme");e||(e=document.createElement("style"),e.id="style-zx-theme",document.head.appendChild(e));let t=`:root {
2
+ `;for(const[s,r]of Object.entries(c))t+=` ${s}: ${r};
3
+ `;return t+="}",e.textContent=t,i.forEach(s=>s(l)),l}function h(){const[n,c]=a.useState(l);return a.useEffect(()=>{const e=t=>c(t);return i.add(e),()=>{i.delete(e)}},[]),n}exports.createTheme=d;exports.useTheme=h;
@@ -0,0 +1 @@
1
+ export {}
package/dist/index.js ADDED
@@ -0,0 +1,38 @@
1
+ import { useState as i, useEffect as a } from "react";
2
+ function d(n, o = "--theme") {
3
+ const e = {};
4
+ function t(s, c) {
5
+ for (const u in s) {
6
+ const r = s[u], f = c ? `${c}-${u}` : u;
7
+ typeof r == "object" && r !== null ? t(r, f) : e[`${o}-${f}`] = r;
8
+ }
9
+ }
10
+ return t(n, ""), e;
11
+ }
12
+ let l = {};
13
+ const m = /* @__PURE__ */ new Set();
14
+ function y(n) {
15
+ l = n;
16
+ const o = d(n);
17
+ let e = document.getElementById("style-zx-theme");
18
+ e || (e = document.createElement("style"), e.id = "style-zx-theme", document.head.appendChild(e));
19
+ let t = `:root {
20
+ `;
21
+ for (const [s, c] of Object.entries(o))
22
+ t += ` ${s}: ${c};
23
+ `;
24
+ return t += "}", e.textContent = t, m.forEach((s) => s(l)), l;
25
+ }
26
+ function $() {
27
+ const [n, o] = i(l);
28
+ return a(() => {
29
+ const e = (t) => o(t);
30
+ return m.add(e), () => {
31
+ m.delete(e);
32
+ };
33
+ }, []), n;
34
+ }
35
+ export {
36
+ y as createTheme,
37
+ $ as useTheme
38
+ };