@sorb/leaf 0.1.0
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 +56 -0
- package/dist/index.js +217 -0
- package/dist/index.js.map +7 -0
- package/dist/index.mjs +185 -0
- package/dist/index.mjs.map +7 -0
- package/package.json +49 -0
package/README.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# @sorb/leaf
|
|
2
|
+
|
|
3
|
+
React provider for [Sorb](https://github.com/nhunsaker/sorb) design
|
|
4
|
+
tokens. Ships in your app bundle — zero runtime dependencies beyond React.
|
|
5
|
+
|
|
6
|
+
```bash
|
|
7
|
+
npm install @sorb/leaf
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
|
|
12
|
+
```jsx
|
|
13
|
+
import { SorbProvider, PreviewBanner } from '@sorb/leaf'
|
|
14
|
+
import { tokens } from './tokens/generated/tokens'
|
|
15
|
+
|
|
16
|
+
const config = {
|
|
17
|
+
namespace: 'my-app',
|
|
18
|
+
tokens,
|
|
19
|
+
preview: {
|
|
20
|
+
enabled: import.meta.env.MODE !== 'production',
|
|
21
|
+
origin: 'http://localhost:7777',
|
|
22
|
+
},
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
<SorbProvider config={config}>
|
|
26
|
+
<App />
|
|
27
|
+
<PreviewBanner /> {/* renders nothing in production */}
|
|
28
|
+
</SorbProvider>
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
> `tokens` is your committed token set — a flat `name → value` object you
|
|
32
|
+
> provide (e.g. `src/tokens/generated/tokens.js` exporting
|
|
33
|
+
> `export const tokens = { 'color-primary': '#3B5BDB', … }`). It's bundled
|
|
34
|
+
> at build time and used in production. See the
|
|
35
|
+
> [main README](https://github.com/nhunsaker/sorb#readme) for generating
|
|
36
|
+
> it with Style Dictionary.
|
|
37
|
+
|
|
38
|
+
When the app is opened with `?preview=<id>`, the provider fetches the
|
|
39
|
+
proposed token set from the local Sorb server, applies it as CSS custom
|
|
40
|
+
properties on `:root`, and polls for live updates. If the server isn't
|
|
41
|
+
running it falls back silently to the committed tokens — preview never
|
|
42
|
+
breaks production.
|
|
43
|
+
|
|
44
|
+
## Hooks
|
|
45
|
+
|
|
46
|
+
```jsx
|
|
47
|
+
import { useToken, useTokens, useIsPreview, usePreviewState } from '@sorb/leaf'
|
|
48
|
+
|
|
49
|
+
const primary = useToken('color-primary') // single token value
|
|
50
|
+
const tokens = useTokens() // full active token set
|
|
51
|
+
const isPreviewing = useIsPreview() // boolean
|
|
52
|
+
const { isPreview, previewId, clearPreview } = usePreviewState()
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
See the [main README](https://github.com/nhunsaker/sorb#readme) for the
|
|
56
|
+
full workflow.
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
|
|
29
|
+
// src/index.js
|
|
30
|
+
var src_exports = {};
|
|
31
|
+
__export(src_exports, {
|
|
32
|
+
PreviewBanner: () => PreviewBanner,
|
|
33
|
+
SorbProvider: () => SorbProvider,
|
|
34
|
+
useIsPreview: () => useIsPreview,
|
|
35
|
+
usePreviewState: () => usePreviewState,
|
|
36
|
+
useToken: () => useToken,
|
|
37
|
+
useTokens: () => useTokens
|
|
38
|
+
});
|
|
39
|
+
module.exports = __toCommonJS(src_exports);
|
|
40
|
+
|
|
41
|
+
// src/TokenProvider.jsx
|
|
42
|
+
var import_react2 = __toESM(require("react"));
|
|
43
|
+
|
|
44
|
+
// src/context.js
|
|
45
|
+
var import_react = require("react");
|
|
46
|
+
var TokenContext = (0, import_react.createContext)(null);
|
|
47
|
+
var useTokenContext = () => {
|
|
48
|
+
const ctx = (0, import_react.useContext)(TokenContext);
|
|
49
|
+
if (!ctx) {
|
|
50
|
+
throw new Error("Sorb hooks must be used inside <SorbProvider>");
|
|
51
|
+
}
|
|
52
|
+
return ctx;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// src/apply.js
|
|
56
|
+
var applyTokens = (tokens) => {
|
|
57
|
+
const root = document.documentElement;
|
|
58
|
+
Object.entries(tokens).forEach(([key, value]) => {
|
|
59
|
+
root.style.setProperty(`--${key}`, String(value));
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// src/TokenProvider.jsx
|
|
64
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
65
|
+
var SorbProvider = ({ config, children }) => {
|
|
66
|
+
const [activeTokens, setActiveTokens] = (0, import_react2.useState)(config.tokens);
|
|
67
|
+
const [isPreview, setIsPreview] = (0, import_react2.useState)(false);
|
|
68
|
+
const [previewId, setPreviewId] = (0, import_react2.useState)(null);
|
|
69
|
+
const pollRef = (0, import_react2.useRef)(null);
|
|
70
|
+
const loadCommitted = (0, import_react2.useCallback)(() => {
|
|
71
|
+
applyTokens(config.tokens);
|
|
72
|
+
setActiveTokens(config.tokens);
|
|
73
|
+
setIsPreview(false);
|
|
74
|
+
setPreviewId(null);
|
|
75
|
+
}, [config.tokens]);
|
|
76
|
+
const loadPreview = (0, import_react2.useCallback)(
|
|
77
|
+
async (id) => {
|
|
78
|
+
const origin = config.preview?.origin ?? "http://localhost:7777";
|
|
79
|
+
try {
|
|
80
|
+
const res = await fetch(`${origin}/preview/${id}`);
|
|
81
|
+
if (!res.ok) throw new Error("preview not found");
|
|
82
|
+
const tokens = await res.json();
|
|
83
|
+
applyTokens(tokens);
|
|
84
|
+
setActiveTokens(tokens);
|
|
85
|
+
setIsPreview(true);
|
|
86
|
+
setPreviewId(id);
|
|
87
|
+
return true;
|
|
88
|
+
} catch {
|
|
89
|
+
loadCommitted();
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
[config.preview?.origin, loadCommitted]
|
|
94
|
+
);
|
|
95
|
+
const clearPreview = (0, import_react2.useCallback)(() => {
|
|
96
|
+
if (pollRef.current) clearInterval(pollRef.current);
|
|
97
|
+
const params = new URLSearchParams(location.search);
|
|
98
|
+
params.delete("preview");
|
|
99
|
+
const qs = params.toString();
|
|
100
|
+
history.replaceState(null, "", qs ? `?${qs}` : location.pathname);
|
|
101
|
+
loadCommitted();
|
|
102
|
+
}, [loadCommitted]);
|
|
103
|
+
(0, import_react2.useEffect)(() => {
|
|
104
|
+
const previewEnabled = config.preview?.enabled ?? false;
|
|
105
|
+
const id = new URLSearchParams(location.search).get("preview");
|
|
106
|
+
if (!previewEnabled || !id) {
|
|
107
|
+
loadCommitted();
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
loadPreview(id).then((ok) => {
|
|
111
|
+
if (!ok) return;
|
|
112
|
+
const interval = config.preview?.pollInterval ?? 1500;
|
|
113
|
+
pollRef.current = setInterval(() => loadPreview(id), interval);
|
|
114
|
+
});
|
|
115
|
+
return () => {
|
|
116
|
+
if (pollRef.current) clearInterval(pollRef.current);
|
|
117
|
+
};
|
|
118
|
+
}, []);
|
|
119
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(TokenContext.Provider, { value: { tokens: activeTokens, isPreview, previewId, clearPreview }, children });
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
// src/PreviewBanner.jsx
|
|
123
|
+
var import_react3 = __toESM(require("react"));
|
|
124
|
+
|
|
125
|
+
// src/hooks.js
|
|
126
|
+
var useTokens = () => {
|
|
127
|
+
return useTokenContext().tokens;
|
|
128
|
+
};
|
|
129
|
+
var useToken = (key) => {
|
|
130
|
+
const tokens = useTokenContext().tokens;
|
|
131
|
+
const value = tokens[key];
|
|
132
|
+
if (value === void 0 && true) {
|
|
133
|
+
console.warn(`[Sorb] Token not found: "${key}"`);
|
|
134
|
+
}
|
|
135
|
+
return String(value ?? "");
|
|
136
|
+
};
|
|
137
|
+
var useIsPreview = () => {
|
|
138
|
+
return useTokenContext().isPreview;
|
|
139
|
+
};
|
|
140
|
+
var usePreviewState = () => {
|
|
141
|
+
const { isPreview, previewId, clearPreview } = useTokenContext();
|
|
142
|
+
return { isPreview, previewId, clearPreview };
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
// src/PreviewBanner.jsx
|
|
146
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
147
|
+
var PreviewBanner = () => {
|
|
148
|
+
const { isPreview, previewId, clearPreview } = usePreviewState();
|
|
149
|
+
if (!isPreview) return null;
|
|
150
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
151
|
+
"div",
|
|
152
|
+
{
|
|
153
|
+
role: "status",
|
|
154
|
+
"aria-live": "polite",
|
|
155
|
+
style: {
|
|
156
|
+
position: "fixed",
|
|
157
|
+
bottom: 0,
|
|
158
|
+
left: 0,
|
|
159
|
+
right: 0,
|
|
160
|
+
background: "#3B5BDB",
|
|
161
|
+
color: "#fff",
|
|
162
|
+
padding: "10px 20px",
|
|
163
|
+
display: "flex",
|
|
164
|
+
alignItems: "center",
|
|
165
|
+
justifyContent: "space-between",
|
|
166
|
+
gap: "12px",
|
|
167
|
+
fontSize: "13px",
|
|
168
|
+
lineHeight: "1.4",
|
|
169
|
+
zIndex: 99999,
|
|
170
|
+
fontFamily: 'system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif',
|
|
171
|
+
boxShadow: "0 -2px 12px rgba(0,0,0,0.15)"
|
|
172
|
+
},
|
|
173
|
+
children: [
|
|
174
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("span", { children: [
|
|
175
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("strong", { style: { fontWeight: 600 }, children: "Sorb preview active" }),
|
|
176
|
+
previewId && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
177
|
+
"code",
|
|
178
|
+
{
|
|
179
|
+
style: {
|
|
180
|
+
marginLeft: "8px",
|
|
181
|
+
opacity: 0.75,
|
|
182
|
+
fontSize: "11px",
|
|
183
|
+
background: "rgba(255,255,255,0.15)",
|
|
184
|
+
padding: "2px 6px",
|
|
185
|
+
borderRadius: "4px"
|
|
186
|
+
},
|
|
187
|
+
children: previewId
|
|
188
|
+
}
|
|
189
|
+
),
|
|
190
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("span", { style: { marginLeft: "8px", opacity: 0.75, fontSize: "12px" }, children: "Token changes from Figma are live" })
|
|
191
|
+
] }),
|
|
192
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
193
|
+
"button",
|
|
194
|
+
{
|
|
195
|
+
onClick: clearPreview,
|
|
196
|
+
style: {
|
|
197
|
+
flexShrink: 0,
|
|
198
|
+
background: "rgba(255,255,255,0.2)",
|
|
199
|
+
border: "1px solid rgba(255,255,255,0.3)",
|
|
200
|
+
color: "#fff",
|
|
201
|
+
padding: "5px 14px",
|
|
202
|
+
borderRadius: "6px",
|
|
203
|
+
cursor: "pointer",
|
|
204
|
+
fontSize: "12px",
|
|
205
|
+
fontWeight: 500,
|
|
206
|
+
transition: "background 0.15s"
|
|
207
|
+
},
|
|
208
|
+
onMouseEnter: (e) => e.target.style.background = "rgba(255,255,255,0.3)",
|
|
209
|
+
onMouseLeave: (e) => e.target.style.background = "rgba(255,255,255,0.2)",
|
|
210
|
+
children: "Exit preview"
|
|
211
|
+
}
|
|
212
|
+
)
|
|
213
|
+
]
|
|
214
|
+
}
|
|
215
|
+
);
|
|
216
|
+
};
|
|
217
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/index.js", "../src/TokenProvider.jsx", "../src/context.js", "../src/apply.js", "../src/PreviewBanner.jsx", "../src/hooks.js"],
|
|
4
|
+
"sourcesContent": ["export { SorbProvider } from './TokenProvider'\nexport { PreviewBanner } from './PreviewBanner'\nexport { useTokens, useToken, useIsPreview, usePreviewState } from './hooks'\n", "import React, { useCallback, useEffect, useRef, useState } from 'react'\nimport { TokenContext } from './context'\nimport { applyTokens } from './apply'\n\n/**\n * @param {{ config: import('./types').SorbConfig, children: React.ReactNode }} props\n */\nexport const SorbProvider = ({ config, children }) => {\n const [activeTokens, setActiveTokens] = useState(config.tokens)\n const [isPreview, setIsPreview] = useState(false)\n const [previewId, setPreviewId] = useState(null)\n const pollRef = useRef(null)\n\n // \u2500\u2500\u2500 committed token loader \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n const loadCommitted = useCallback(() => {\n applyTokens(config.tokens)\n setActiveTokens(config.tokens)\n setIsPreview(false)\n setPreviewId(null)\n }, [config.tokens])\n\n // \u2500\u2500\u2500 preview token loader \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n const loadPreview = useCallback(\n async (id) => {\n const origin = config.preview?.origin ?? 'http://localhost:7777'\n try {\n const res = await fetch(`${origin}/preview/${id}`)\n if (!res.ok) throw new Error('preview not found')\n const tokens = await res.json()\n applyTokens(tokens)\n setActiveTokens(tokens)\n setIsPreview(true)\n setPreviewId(id)\n return true\n } catch {\n // local server not running, preview expired, or network error\n // fall back silently \u2014 never break the app\n loadCommitted()\n return false\n }\n },\n [config.preview?.origin, loadCommitted],\n )\n\n // \u2500\u2500\u2500 clear preview + remove query param \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n const clearPreview = useCallback(() => {\n if (pollRef.current) clearInterval(pollRef.current)\n const params = new URLSearchParams(location.search)\n params.delete('preview')\n const qs = params.toString()\n history.replaceState(null, '', qs ? `?${qs}` : location.pathname)\n loadCommitted()\n }, [loadCommitted])\n\n // \u2500\u2500\u2500 initialise on mount \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n useEffect(() => {\n const previewEnabled = config.preview?.enabled ?? false\n const id = new URLSearchParams(location.search).get('preview')\n\n // bail out immediately if preview is disabled or no param present\n if (!previewEnabled || !id) {\n loadCommitted()\n return\n }\n\n // load the preview, then start polling so Figma changes reflect live\n loadPreview(id).then((ok) => {\n if (!ok) return\n const interval = config.preview?.pollInterval ?? 1500\n pollRef.current = setInterval(() => loadPreview(id), interval)\n })\n\n return () => {\n if (pollRef.current) clearInterval(pollRef.current)\n }\n }, []) // intentionally empty \u2014 only runs on mount\n\n return (\n <TokenContext.Provider value={{ tokens: activeTokens, isPreview, previewId, clearPreview }}>\n {children}\n </TokenContext.Provider>\n )\n}\n", "import { createContext, useContext } from 'react'\n\n/** @type {import('react').Context<import('./types').TokenContextValue | null>} */\nexport const TokenContext = createContext(null)\n\n/** @returns {import('./types').TokenContextValue} */\nexport const useTokenContext = () => {\n const ctx = useContext(TokenContext)\n if (!ctx) {\n throw new Error('Sorb hooks must be used inside <SorbProvider>')\n }\n return ctx\n}\n", "/**\n * Writes all token values as CSS custom properties on :root.\n * Applies globally \u2014 affects the entire app.\n *\n * @param {import('./types').TokenSet} tokens\n * @returns {void}\n */\nexport const applyTokens = (tokens) => {\n const root = document.documentElement\n Object.entries(tokens).forEach(([key, value]) => {\n root.style.setProperty(`--${key}`, String(value))\n })\n}\n\n/**\n * Removes token CSS custom properties from :root.\n * Called when clearing a preview to restore the committed set.\n *\n * @param {import('./types').TokenSet} tokens\n * @returns {void}\n */\nexport const clearTokenOverrides = (tokens) => {\n const root = document.documentElement\n Object.keys(tokens).forEach((key) => {\n root.style.removeProperty(`--${key}`)\n })\n}\n", "import React from 'react'\nimport { usePreviewState } from './hooks'\n\n/**\n * Drop-in banner that appears at the bottom of the screen when a\n * Sorb preview is active. Includes an \"Exit preview\" button.\n *\n * Only renders when preview.enabled is true AND a preview is loaded.\n * Safe to include unconditionally \u2014 renders nothing in production.\n *\n * @example\n * // In your app root, after <SorbProvider>\n * <PreviewBanner />\n */\nexport const PreviewBanner = () => {\n const { isPreview, previewId, clearPreview } = usePreviewState()\n if (!isPreview) return null\n\n return (\n <div\n role=\"status\"\n aria-live=\"polite\"\n style={{\n position: 'fixed',\n bottom: 0,\n left: 0,\n right: 0,\n background: '#3B5BDB',\n color: '#fff',\n padding: '10px 20px',\n display: 'flex',\n alignItems: 'center',\n justifyContent: 'space-between',\n gap: '12px',\n fontSize: '13px',\n lineHeight: '1.4',\n zIndex: 99999,\n fontFamily:\n 'system-ui, -apple-system, BlinkMacSystemFont, \"Segoe UI\", sans-serif',\n boxShadow: '0 -2px 12px rgba(0,0,0,0.15)',\n }}\n >\n <span>\n <strong style={{ fontWeight: 600 }}>Sorb preview active</strong>\n {previewId && (\n <code\n style={{\n marginLeft: '8px',\n opacity: 0.75,\n fontSize: '11px',\n background: 'rgba(255,255,255,0.15)',\n padding: '2px 6px',\n borderRadius: '4px',\n }}\n >\n {previewId}\n </code>\n )}\n <span style={{ marginLeft: '8px', opacity: 0.75, fontSize: '12px' }}>\n Token changes from Figma are live\n </span>\n </span>\n <button\n onClick={clearPreview}\n style={{\n flexShrink: 0,\n background: 'rgba(255,255,255,0.2)',\n border: '1px solid rgba(255,255,255,0.3)',\n color: '#fff',\n padding: '5px 14px',\n borderRadius: '6px',\n cursor: 'pointer',\n fontSize: '12px',\n fontWeight: 500,\n transition: 'background 0.15s',\n }}\n onMouseEnter={(e) =>\n (e.target.style.background = 'rgba(255,255,255,0.3)')\n }\n onMouseLeave={(e) =>\n (e.target.style.background = 'rgba(255,255,255,0.2)')\n }\n >\n Exit preview\n </button>\n </div>\n )\n}\n", "import { useTokenContext } from './context'\n\n/**\n * Returns the full active token set (committed or preview).\n * @returns {import('./types').TokenSet}\n */\nexport const useTokens = () => {\n return useTokenContext().tokens\n}\n\n/**\n * Returns a single token value by key.\n *\n * @param {string} key\n * @returns {string}\n * @example\n * const primary = useToken('color-primary') // \u2192 '#3B5BDB'\n */\nexport const useToken = (key) => {\n const tokens = useTokenContext().tokens\n const value = tokens[key]\n if (value === undefined && process.env.NODE_ENV === 'development') {\n console.warn(`[Sorb] Token not found: \"${key}\"`)\n }\n return String(value ?? '')\n}\n\n/**\n * Returns whether a preview token set is currently active.\n * Useful for showing a preview indicator in your app.\n * @returns {boolean}\n */\nexport const useIsPreview = () => {\n return useTokenContext().isPreview\n}\n\n/**\n * Returns full preview state \u2014 useful for building a preview banner.\n *\n * @example\n * const { isPreview, previewId, clearPreview } = usePreviewState()\n */\nexport const usePreviewState = () => {\n const { isPreview, previewId, clearPreview } = useTokenContext()\n return { isPreview, previewId, clearPreview }\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,gBAAgE;;;ACAhE,mBAA0C;AAGnC,IAAM,mBAAe,4BAAc,IAAI;AAGvC,IAAM,kBAAkB,MAAM;AACnC,QAAM,UAAM,yBAAW,YAAY;AACnC,MAAI,CAAC,KAAK;AACR,UAAM,IAAI,MAAM,+CAA+C;AAAA,EACjE;AACA,SAAO;AACT;;;ACLO,IAAM,cAAc,CAAC,WAAW;AACrC,QAAM,OAAO,SAAS;AACtB,SAAO,QAAQ,MAAM,EAAE,QAAQ,CAAC,CAAC,KAAK,KAAK,MAAM;AAC/C,SAAK,MAAM,YAAY,KAAK,GAAG,IAAI,OAAO,KAAK,CAAC;AAAA,EAClD,CAAC;AACH;;;AFkEI;AAvEG,IAAM,eAAe,CAAC,EAAE,QAAQ,SAAS,MAAM;AACpD,QAAM,CAAC,cAAc,eAAe,Q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QACT,SAAS;AAAA,QACT,YAAY;AAAA,QACZ,gBAAgB;AAAA,QAChB,KAAK;AAAA,QACL,UAAU;AAAA,QACV,YAAY;AAAA,QACZ,QAAQ;AAAA,QACR,YACE;AAAA,QACF,WAAW;AAAA,MACb;AAAA,MAEA;AAAA,sDAAC,UACC;AAAA,uDAAC,YAAO,OAAO,EAAE,YAAY,IAAI,GAAG,iCAAmB;AAAA,UACtD,aACC;AAAA,YAAC;AAAA;AAAA,cACC,OAAO;AAAA,gBACL,YAAY;AAAA,gBACZ,SAAS;AAAA,gBACT,UAAU;AAAA,gBACV,YAAY;AAAA,gBACZ,SAAS;AAAA,gBACT,cAAc;AAAA,cAChB;AAAA,cAEC;AAAA;AAAA,UACH;AAAA,UAEF,6CAAC,UAAK,OAAO,EAAE,YAAY,OAAO,SAAS,MAAM,UAAU,OAAO,GAAG,+CAErE;AAAA,WACF;AAAA,QACA;AAAA,UAAC;AAAA;AAAA,YACC,SAAS;AAAA,YACT,OAAO;AAAA,cACL,YAAY;AAAA,cACZ,YAAY;AAAA,cACZ,QAAQ;AAAA,cACR,OAAO;AAAA,cACP,SAAS;AAAA,cACT,cAAc;AAAA,cACd,QAAQ;AAAA,cACR,UAAU;AAAA,cACV,YAAY;AAAA,cACZ,YAAY;AAAA,YACd;AAAA,YACA,cAAc,CAAC,MACZ,EAAE,OAAO,MAAM,aAAa;AAAA,YAE/B,cAAc,CAAC,MACZ,EAAE,OAAO,MAAM,aAAa;AAAA,YAEhC;AAAA;AAAA,QAED;AAAA;AAAA;AAAA,EACF;AAEJ;",
|
|
6
|
+
"names": ["import_react", "import_react", "import_jsx_runtime"]
|
|
7
|
+
}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
// src/TokenProvider.jsx
|
|
2
|
+
import React, { useCallback, useEffect, useRef, useState } from "react";
|
|
3
|
+
|
|
4
|
+
// src/context.js
|
|
5
|
+
import { createContext, useContext } from "react";
|
|
6
|
+
var TokenContext = createContext(null);
|
|
7
|
+
var useTokenContext = () => {
|
|
8
|
+
const ctx = useContext(TokenContext);
|
|
9
|
+
if (!ctx) {
|
|
10
|
+
throw new Error("Sorb hooks must be used inside <SorbProvider>");
|
|
11
|
+
}
|
|
12
|
+
return ctx;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
// src/apply.js
|
|
16
|
+
var applyTokens = (tokens) => {
|
|
17
|
+
const root = document.documentElement;
|
|
18
|
+
Object.entries(tokens).forEach(([key, value]) => {
|
|
19
|
+
root.style.setProperty(`--${key}`, String(value));
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// src/TokenProvider.jsx
|
|
24
|
+
import { jsx } from "react/jsx-runtime";
|
|
25
|
+
var SorbProvider = ({ config, children }) => {
|
|
26
|
+
const [activeTokens, setActiveTokens] = useState(config.tokens);
|
|
27
|
+
const [isPreview, setIsPreview] = useState(false);
|
|
28
|
+
const [previewId, setPreviewId] = useState(null);
|
|
29
|
+
const pollRef = useRef(null);
|
|
30
|
+
const loadCommitted = useCallback(() => {
|
|
31
|
+
applyTokens(config.tokens);
|
|
32
|
+
setActiveTokens(config.tokens);
|
|
33
|
+
setIsPreview(false);
|
|
34
|
+
setPreviewId(null);
|
|
35
|
+
}, [config.tokens]);
|
|
36
|
+
const loadPreview = useCallback(
|
|
37
|
+
async (id) => {
|
|
38
|
+
const origin = config.preview?.origin ?? "http://localhost:7777";
|
|
39
|
+
try {
|
|
40
|
+
const res = await fetch(`${origin}/preview/${id}`);
|
|
41
|
+
if (!res.ok) throw new Error("preview not found");
|
|
42
|
+
const tokens = await res.json();
|
|
43
|
+
applyTokens(tokens);
|
|
44
|
+
setActiveTokens(tokens);
|
|
45
|
+
setIsPreview(true);
|
|
46
|
+
setPreviewId(id);
|
|
47
|
+
return true;
|
|
48
|
+
} catch {
|
|
49
|
+
loadCommitted();
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
[config.preview?.origin, loadCommitted]
|
|
54
|
+
);
|
|
55
|
+
const clearPreview = useCallback(() => {
|
|
56
|
+
if (pollRef.current) clearInterval(pollRef.current);
|
|
57
|
+
const params = new URLSearchParams(location.search);
|
|
58
|
+
params.delete("preview");
|
|
59
|
+
const qs = params.toString();
|
|
60
|
+
history.replaceState(null, "", qs ? `?${qs}` : location.pathname);
|
|
61
|
+
loadCommitted();
|
|
62
|
+
}, [loadCommitted]);
|
|
63
|
+
useEffect(() => {
|
|
64
|
+
const previewEnabled = config.preview?.enabled ?? false;
|
|
65
|
+
const id = new URLSearchParams(location.search).get("preview");
|
|
66
|
+
if (!previewEnabled || !id) {
|
|
67
|
+
loadCommitted();
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
loadPreview(id).then((ok) => {
|
|
71
|
+
if (!ok) return;
|
|
72
|
+
const interval = config.preview?.pollInterval ?? 1500;
|
|
73
|
+
pollRef.current = setInterval(() => loadPreview(id), interval);
|
|
74
|
+
});
|
|
75
|
+
return () => {
|
|
76
|
+
if (pollRef.current) clearInterval(pollRef.current);
|
|
77
|
+
};
|
|
78
|
+
}, []);
|
|
79
|
+
return /* @__PURE__ */ jsx(TokenContext.Provider, { value: { tokens: activeTokens, isPreview, previewId, clearPreview }, children });
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
// src/PreviewBanner.jsx
|
|
83
|
+
import React2 from "react";
|
|
84
|
+
|
|
85
|
+
// src/hooks.js
|
|
86
|
+
var useTokens = () => {
|
|
87
|
+
return useTokenContext().tokens;
|
|
88
|
+
};
|
|
89
|
+
var useToken = (key) => {
|
|
90
|
+
const tokens = useTokenContext().tokens;
|
|
91
|
+
const value = tokens[key];
|
|
92
|
+
if (value === void 0 && true) {
|
|
93
|
+
console.warn(`[Sorb] Token not found: "${key}"`);
|
|
94
|
+
}
|
|
95
|
+
return String(value ?? "");
|
|
96
|
+
};
|
|
97
|
+
var useIsPreview = () => {
|
|
98
|
+
return useTokenContext().isPreview;
|
|
99
|
+
};
|
|
100
|
+
var usePreviewState = () => {
|
|
101
|
+
const { isPreview, previewId, clearPreview } = useTokenContext();
|
|
102
|
+
return { isPreview, previewId, clearPreview };
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
// src/PreviewBanner.jsx
|
|
106
|
+
import { jsx as jsx2, jsxs } from "react/jsx-runtime";
|
|
107
|
+
var PreviewBanner = () => {
|
|
108
|
+
const { isPreview, previewId, clearPreview } = usePreviewState();
|
|
109
|
+
if (!isPreview) return null;
|
|
110
|
+
return /* @__PURE__ */ jsxs(
|
|
111
|
+
"div",
|
|
112
|
+
{
|
|
113
|
+
role: "status",
|
|
114
|
+
"aria-live": "polite",
|
|
115
|
+
style: {
|
|
116
|
+
position: "fixed",
|
|
117
|
+
bottom: 0,
|
|
118
|
+
left: 0,
|
|
119
|
+
right: 0,
|
|
120
|
+
background: "#3B5BDB",
|
|
121
|
+
color: "#fff",
|
|
122
|
+
padding: "10px 20px",
|
|
123
|
+
display: "flex",
|
|
124
|
+
alignItems: "center",
|
|
125
|
+
justifyContent: "space-between",
|
|
126
|
+
gap: "12px",
|
|
127
|
+
fontSize: "13px",
|
|
128
|
+
lineHeight: "1.4",
|
|
129
|
+
zIndex: 99999,
|
|
130
|
+
fontFamily: 'system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif',
|
|
131
|
+
boxShadow: "0 -2px 12px rgba(0,0,0,0.15)"
|
|
132
|
+
},
|
|
133
|
+
children: [
|
|
134
|
+
/* @__PURE__ */ jsxs("span", { children: [
|
|
135
|
+
/* @__PURE__ */ jsx2("strong", { style: { fontWeight: 600 }, children: "Sorb preview active" }),
|
|
136
|
+
previewId && /* @__PURE__ */ jsx2(
|
|
137
|
+
"code",
|
|
138
|
+
{
|
|
139
|
+
style: {
|
|
140
|
+
marginLeft: "8px",
|
|
141
|
+
opacity: 0.75,
|
|
142
|
+
fontSize: "11px",
|
|
143
|
+
background: "rgba(255,255,255,0.15)",
|
|
144
|
+
padding: "2px 6px",
|
|
145
|
+
borderRadius: "4px"
|
|
146
|
+
},
|
|
147
|
+
children: previewId
|
|
148
|
+
}
|
|
149
|
+
),
|
|
150
|
+
/* @__PURE__ */ jsx2("span", { style: { marginLeft: "8px", opacity: 0.75, fontSize: "12px" }, children: "Token changes from Figma are live" })
|
|
151
|
+
] }),
|
|
152
|
+
/* @__PURE__ */ jsx2(
|
|
153
|
+
"button",
|
|
154
|
+
{
|
|
155
|
+
onClick: clearPreview,
|
|
156
|
+
style: {
|
|
157
|
+
flexShrink: 0,
|
|
158
|
+
background: "rgba(255,255,255,0.2)",
|
|
159
|
+
border: "1px solid rgba(255,255,255,0.3)",
|
|
160
|
+
color: "#fff",
|
|
161
|
+
padding: "5px 14px",
|
|
162
|
+
borderRadius: "6px",
|
|
163
|
+
cursor: "pointer",
|
|
164
|
+
fontSize: "12px",
|
|
165
|
+
fontWeight: 500,
|
|
166
|
+
transition: "background 0.15s"
|
|
167
|
+
},
|
|
168
|
+
onMouseEnter: (e) => e.target.style.background = "rgba(255,255,255,0.3)",
|
|
169
|
+
onMouseLeave: (e) => e.target.style.background = "rgba(255,255,255,0.2)",
|
|
170
|
+
children: "Exit preview"
|
|
171
|
+
}
|
|
172
|
+
)
|
|
173
|
+
]
|
|
174
|
+
}
|
|
175
|
+
);
|
|
176
|
+
};
|
|
177
|
+
export {
|
|
178
|
+
PreviewBanner,
|
|
179
|
+
SorbProvider,
|
|
180
|
+
useIsPreview,
|
|
181
|
+
usePreviewState,
|
|
182
|
+
useToken,
|
|
183
|
+
useTokens
|
|
184
|
+
};
|
|
185
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/TokenProvider.jsx", "../src/context.js", "../src/apply.js", "../src/PreviewBanner.jsx", "../src/hooks.js"],
|
|
4
|
+
"sourcesContent": ["import React, { useCallback, useEffect, useRef, useState } from 'react'\nimport { TokenContext } from './context'\nimport { applyTokens } from './apply'\n\n/**\n * @param {{ config: import('./types').SorbConfig, children: React.ReactNode }} props\n */\nexport const SorbProvider = ({ config, children }) => {\n const [activeTokens, setActiveTokens] = useState(config.tokens)\n const [isPreview, setIsPreview] = useState(false)\n const [previewId, setPreviewId] = useState(null)\n const pollRef = useRef(null)\n\n // \u2500\u2500\u2500 committed token loader \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n const loadCommitted = useCallback(() => {\n applyTokens(config.tokens)\n setActiveTokens(config.tokens)\n setIsPreview(false)\n setPreviewId(null)\n }, [config.tokens])\n\n // \u2500\u2500\u2500 preview token loader \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n const loadPreview = useCallback(\n async (id) => {\n const origin = config.preview?.origin ?? 'http://localhost:7777'\n try {\n const res = await fetch(`${origin}/preview/${id}`)\n if (!res.ok) throw new Error('preview not found')\n const tokens = await res.json()\n applyTokens(tokens)\n setActiveTokens(tokens)\n setIsPreview(true)\n setPreviewId(id)\n return true\n } catch {\n // local server not running, preview expired, or network error\n // fall back silently \u2014 never break the app\n loadCommitted()\n return false\n }\n },\n [config.preview?.origin, loadCommitted],\n )\n\n // \u2500\u2500\u2500 clear preview + remove query param \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n const clearPreview = useCallback(() => {\n if (pollRef.current) clearInterval(pollRef.current)\n const params = new URLSearchParams(location.search)\n params.delete('preview')\n const qs = params.toString()\n history.replaceState(null, '', qs ? `?${qs}` : location.pathname)\n loadCommitted()\n }, [loadCommitted])\n\n // \u2500\u2500\u2500 initialise on mount \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n useEffect(() => {\n const previewEnabled = config.preview?.enabled ?? false\n const id = new URLSearchParams(location.search).get('preview')\n\n // bail out immediately if preview is disabled or no param present\n if (!previewEnabled || !id) {\n loadCommitted()\n return\n }\n\n // load the preview, then start polling so Figma changes reflect live\n loadPreview(id).then((ok) => {\n if (!ok) return\n const interval = config.preview?.pollInterval ?? 1500\n pollRef.current = setInterval(() => loadPreview(id), interval)\n })\n\n return () => {\n if (pollRef.current) clearInterval(pollRef.current)\n }\n }, []) // intentionally empty \u2014 only runs on mount\n\n return (\n <TokenContext.Provider value={{ tokens: activeTokens, isPreview, previewId, clearPreview }}>\n {children}\n </TokenContext.Provider>\n )\n}\n", "import { createContext, useContext } from 'react'\n\n/** @type {import('react').Context<import('./types').TokenContextValue | null>} */\nexport const TokenContext = createContext(null)\n\n/** @returns {import('./types').TokenContextValue} */\nexport const useTokenContext = () => {\n const ctx = useContext(TokenContext)\n if (!ctx) {\n throw new Error('Sorb hooks must be used inside <SorbProvider>')\n }\n return ctx\n}\n", "/**\n * Writes all token values as CSS custom properties on :root.\n * Applies globally \u2014 affects the entire app.\n *\n * @param {import('./types').TokenSet} tokens\n * @returns {void}\n */\nexport const applyTokens = (tokens) => {\n const root = document.documentElement\n Object.entries(tokens).forEach(([key, value]) => {\n root.style.setProperty(`--${key}`, String(value))\n })\n}\n\n/**\n * Removes token CSS custom properties from :root.\n * Called when clearing a preview to restore the committed set.\n *\n * @param {import('./types').TokenSet} tokens\n * @returns {void}\n */\nexport const clearTokenOverrides = (tokens) => {\n const root = document.documentElement\n Object.keys(tokens).forEach((key) => {\n root.style.removeProperty(`--${key}`)\n })\n}\n", "import React from 'react'\nimport { usePreviewState } from './hooks'\n\n/**\n * Drop-in banner that appears at the bottom of the screen when a\n * Sorb preview is active. Includes an \"Exit preview\" button.\n *\n * Only renders when preview.enabled is true AND a preview is loaded.\n * Safe to include unconditionally \u2014 renders nothing in production.\n *\n * @example\n * // In your app root, after <SorbProvider>\n * <PreviewBanner />\n */\nexport const PreviewBanner = () => {\n const { isPreview, previewId, clearPreview } = usePreviewState()\n if (!isPreview) return null\n\n return (\n <div\n role=\"status\"\n aria-live=\"polite\"\n style={{\n position: 'fixed',\n bottom: 0,\n left: 0,\n right: 0,\n background: '#3B5BDB',\n color: '#fff',\n padding: '10px 20px',\n display: 'flex',\n alignItems: 'center',\n justifyContent: 'space-between',\n gap: '12px',\n fontSize: '13px',\n lineHeight: '1.4',\n zIndex: 99999,\n fontFamily:\n 'system-ui, -apple-system, BlinkMacSystemFont, \"Segoe UI\", sans-serif',\n boxShadow: '0 -2px 12px rgba(0,0,0,0.15)',\n }}\n >\n <span>\n <strong style={{ fontWeight: 600 }}>Sorb preview active</strong>\n {previewId && (\n <code\n style={{\n marginLeft: '8px',\n opacity: 0.75,\n fontSize: '11px',\n background: 'rgba(255,255,255,0.15)',\n padding: '2px 6px',\n borderRadius: '4px',\n }}\n >\n {previewId}\n </code>\n )}\n <span style={{ marginLeft: '8px', opacity: 0.75, fontSize: '12px' }}>\n Token changes from Figma are live\n </span>\n </span>\n <button\n onClick={clearPreview}\n style={{\n flexShrink: 0,\n background: 'rgba(255,255,255,0.2)',\n border: '1px solid rgba(255,255,255,0.3)',\n color: '#fff',\n padding: '5px 14px',\n borderRadius: '6px',\n cursor: 'pointer',\n fontSize: '12px',\n fontWeight: 500,\n transition: 'background 0.15s',\n }}\n onMouseEnter={(e) =>\n (e.target.style.background = 'rgba(255,255,255,0.3)')\n }\n onMouseLeave={(e) =>\n (e.target.style.background = 'rgba(255,255,255,0.2)')\n }\n >\n Exit preview\n </button>\n </div>\n )\n}\n", "import { useTokenContext } from './context'\n\n/**\n * Returns the full active token set (committed or preview).\n * @returns {import('./types').TokenSet}\n */\nexport const useTokens = () => {\n return useTokenContext().tokens\n}\n\n/**\n * Returns a single token value by key.\n *\n * @param {string} key\n * @returns {string}\n * @example\n * const primary = useToken('color-primary') // \u2192 '#3B5BDB'\n */\nexport const useToken = (key) => {\n const tokens = useTokenContext().tokens\n const value = tokens[key]\n if (value === undefined && process.env.NODE_ENV === 'development') {\n console.warn(`[Sorb] Token not found: \"${key}\"`)\n }\n return String(value ?? '')\n}\n\n/**\n * Returns whether a preview token set is currently active.\n * Useful for showing a preview indicator in your app.\n * @returns {boolean}\n */\nexport const useIsPreview = () => {\n return useTokenContext().isPreview\n}\n\n/**\n * Returns full preview state \u2014 useful for building a preview banner.\n *\n * @example\n * const { isPreview, previewId, clearPreview } = usePreviewState()\n */\nexport const usePreviewState = () => {\n const { isPreview, previewId, clearPreview } = useTokenContext()\n return { isPreview, previewId, clearPreview }\n}\n"],
|
|
5
|
+
"mappings": ";AAAA,OAAO,SAAS,aAAa,WAAW,QAAQ,gBAAgB;;;ACAhE,SAAS,eAAe,kBAAkB;AAGnC,IAAM,eAAe,cAAc,IAAI;AAGvC,IAAM,kBAAkB,MAAM;AACnC,QAAM,MAAM,WAAW,YAAY;AACnC,MAAI,CAAC,KAAK;AACR,UAAM,IAAI,MAAM,+CAA+C;AAAA,EACjE;AACA,SAAO;AACT;;;ACLO,IAAM,cAAc,CAAC,WAAW;AACrC,QAAM,OAAO,SAAS;AACtB,SAAO,QAAQ,MAAM,EAAE,QAAQ,CAAC,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
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sorb/leaf",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "React provider for Sorb design tokens — the foliage rendered in your running app",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"sorb",
|
|
8
|
+
"design-tokens",
|
|
9
|
+
"figma",
|
|
10
|
+
"react",
|
|
11
|
+
"tokens"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/metatoy/sorb-leaf#readme",
|
|
14
|
+
"bugs": "https://github.com/metatoy/sorb-leaf/issues",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/metatoy/sorb-leaf.git"
|
|
18
|
+
},
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"main": "dist/index.js",
|
|
23
|
+
"module": "dist/index.mjs",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"import": "./dist/index.mjs",
|
|
27
|
+
"require": "./dist/index.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist"
|
|
32
|
+
],
|
|
33
|
+
"sideEffects": false,
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "node build.mjs",
|
|
36
|
+
"dev": "node build.mjs --watch",
|
|
37
|
+
"prepublishOnly": "node build.mjs"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@sorb/core": "^0.1.0"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"react": "^18.0.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"esbuild": "^0.21.0",
|
|
47
|
+
"react": "^18.0.0"
|
|
48
|
+
}
|
|
49
|
+
}
|