@spark-web/box 0.0.0-snapshot-release-20260409001813
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/CHANGELOG.md +696 -0
- package/CLAUDE.md +87 -0
- package/README.md +143 -0
- package/dist/declarations/src/background-provider.d.ts +7 -0
- package/dist/declarations/src/box.d.ts +48 -0
- package/dist/declarations/src/context.d.ts +7 -0
- package/dist/declarations/src/index.d.ts +5 -0
- package/dist/declarations/src/use-box-styles.d.ts +227 -0
- package/dist/spark-web-box.cjs.d.ts +2 -0
- package/dist/spark-web-box.cjs.dev.js +360 -0
- package/dist/spark-web-box.cjs.js +7 -0
- package/dist/spark-web-box.cjs.prod.js +360 -0
- package/dist/spark-web-box.esm.js +353 -0
- package/package.json +35 -0
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
import { useTheme } from '@spark-web/theme';
|
|
2
|
+
import { createContext, useContext } from 'react';
|
|
3
|
+
import { jsx } from '@emotion/react/jsx-runtime';
|
|
4
|
+
import _objectSpread from '@babel/runtime/helpers/esm/objectSpread2';
|
|
5
|
+
import _objectWithoutProperties from '@babel/runtime/helpers/esm/objectWithoutProperties';
|
|
6
|
+
import { css } from '@emotion/react';
|
|
7
|
+
import { resetElementStyles, buildDataAttributes } from '@spark-web/utils/internal';
|
|
8
|
+
import { forwardRefWithAs } from '@spark-web/utils/ts';
|
|
9
|
+
|
|
10
|
+
// prepare context
|
|
11
|
+
|
|
12
|
+
var backgroundContext = /*#__PURE__*/createContext('body');
|
|
13
|
+
var InternalBackgroundProvider = backgroundContext.Provider;
|
|
14
|
+
var useBackground = function useBackground() {
|
|
15
|
+
return useContext(backgroundContext);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// conditional provider
|
|
19
|
+
|
|
20
|
+
function renderBackgroundProvider(background, element) {
|
|
21
|
+
return background ? jsx(InternalBackgroundProvider, {
|
|
22
|
+
value: background,
|
|
23
|
+
children: element
|
|
24
|
+
}) : element;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// a11y contrast utility
|
|
28
|
+
|
|
29
|
+
var useBackgroundLightness = function useBackgroundLightness(backgroundOverride) {
|
|
30
|
+
var backgroundFromContext = useBackground();
|
|
31
|
+
var background = backgroundOverride || backgroundFromContext;
|
|
32
|
+
var theme = useTheme();
|
|
33
|
+
var defaultLightness = theme.backgroundLightness.body;
|
|
34
|
+
|
|
35
|
+
// used by the consumer-facing/external BackgroundProvider
|
|
36
|
+
if (background === 'UNKNOWN_DARK') {
|
|
37
|
+
return 'dark';
|
|
38
|
+
}
|
|
39
|
+
if (background === 'UNKNOWN_LIGHT') {
|
|
40
|
+
return 'light';
|
|
41
|
+
}
|
|
42
|
+
return background ? theme.backgroundLightness[background] || defaultLightness : defaultLightness;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/** Enforce background "lightness" without applying a background color. */
|
|
46
|
+
var BackgroundProvider = function BackgroundProvider(_ref) {
|
|
47
|
+
var type = _ref.type,
|
|
48
|
+
children = _ref.children;
|
|
49
|
+
return renderBackgroundProvider(type === 'dark' ? 'UNKNOWN_DARK' : 'UNKNOWN_LIGHT', children);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
function isObjKey(key, obj) {
|
|
53
|
+
return key in obj;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// TODO perf review
|
|
57
|
+
// TODO: review responsive props! Now that we're using object syntax, un-mapped properties don't behave as expected
|
|
58
|
+
|
|
59
|
+
// types
|
|
60
|
+
|
|
61
|
+
// Hook
|
|
62
|
+
// ------------------------------
|
|
63
|
+
|
|
64
|
+
var useBoxStyles = function useBoxStyles(_ref) {
|
|
65
|
+
var alignItems = _ref.alignItems,
|
|
66
|
+
alignSelf = _ref.alignSelf,
|
|
67
|
+
background = _ref.background,
|
|
68
|
+
border = _ref.border,
|
|
69
|
+
borderTop = _ref.borderTop,
|
|
70
|
+
borderBottom = _ref.borderBottom,
|
|
71
|
+
borderLeft = _ref.borderLeft,
|
|
72
|
+
borderRight = _ref.borderRight,
|
|
73
|
+
borderRadius = _ref.borderRadius,
|
|
74
|
+
_ref$borderWidth = _ref.borderWidth,
|
|
75
|
+
borderWidth = _ref$borderWidth === void 0 ? 'standard' : _ref$borderWidth,
|
|
76
|
+
bottom = _ref.bottom,
|
|
77
|
+
cursor = _ref.cursor,
|
|
78
|
+
display = _ref.display,
|
|
79
|
+
flex = _ref.flex,
|
|
80
|
+
flexDirection = _ref.flexDirection,
|
|
81
|
+
flexGrow = _ref.flexGrow,
|
|
82
|
+
flexShrink = _ref.flexShrink,
|
|
83
|
+
flexWrap = _ref.flexWrap,
|
|
84
|
+
gap = _ref.gap,
|
|
85
|
+
height = _ref.height,
|
|
86
|
+
justifyContent = _ref.justifyContent,
|
|
87
|
+
left = _ref.left,
|
|
88
|
+
margin = _ref.margin,
|
|
89
|
+
marginBottom = _ref.marginBottom,
|
|
90
|
+
marginLeft = _ref.marginLeft,
|
|
91
|
+
marginRight = _ref.marginRight,
|
|
92
|
+
marginTop = _ref.marginTop,
|
|
93
|
+
marginX = _ref.marginX,
|
|
94
|
+
marginY = _ref.marginY,
|
|
95
|
+
minHeight = _ref.minHeight,
|
|
96
|
+
minWidth = _ref.minWidth,
|
|
97
|
+
opacity = _ref.opacity,
|
|
98
|
+
overflow = _ref.overflow,
|
|
99
|
+
padding = _ref.padding,
|
|
100
|
+
paddingBottom = _ref.paddingBottom,
|
|
101
|
+
paddingLeft = _ref.paddingLeft,
|
|
102
|
+
paddingRight = _ref.paddingRight,
|
|
103
|
+
paddingTop = _ref.paddingTop,
|
|
104
|
+
paddingX = _ref.paddingX,
|
|
105
|
+
paddingY = _ref.paddingY,
|
|
106
|
+
position = _ref.position,
|
|
107
|
+
right = _ref.right,
|
|
108
|
+
shadow = _ref.shadow,
|
|
109
|
+
top = _ref.top,
|
|
110
|
+
userSelect = _ref.userSelect,
|
|
111
|
+
width = _ref.width,
|
|
112
|
+
zIndex = _ref.zIndex;
|
|
113
|
+
var theme = useTheme();
|
|
114
|
+
var convertShadowValue = function convertShadowValue(shadow) {
|
|
115
|
+
if (!shadow) {
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
if (isObjKey(shadow, theme.shadow)) {
|
|
119
|
+
return theme.shadow[shadow];
|
|
120
|
+
}
|
|
121
|
+
return shadow;
|
|
122
|
+
};
|
|
123
|
+
var unresponsiveProps = {
|
|
124
|
+
background: background ? theme.color.background[background] : undefined,
|
|
125
|
+
boxShadow: convertShadowValue(shadow),
|
|
126
|
+
cursor: cursor,
|
|
127
|
+
minHeight: minHeight,
|
|
128
|
+
minWidth: minWidth,
|
|
129
|
+
opacity: opacity,
|
|
130
|
+
overflow: overflow,
|
|
131
|
+
userSelect: userSelect
|
|
132
|
+
};
|
|
133
|
+
var convertBorderValue = function convertBorderValue(value, scaleDefinition) {
|
|
134
|
+
if (!(typeof value === 'string' && value in scaleDefinition)) {
|
|
135
|
+
return value;
|
|
136
|
+
}
|
|
137
|
+
return theme.utils.mapResponsiveScale(value, scaleDefinition);
|
|
138
|
+
};
|
|
139
|
+
var conditionalBorderStyles = _objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread({}, border ? {
|
|
140
|
+
borderStyle: 'solid',
|
|
141
|
+
borderColor: convertBorderValue(border, theme.border.color),
|
|
142
|
+
borderWidth: theme.utils.mapResponsiveScale(borderWidth, theme.border.width)
|
|
143
|
+
} : {}), borderTop ? {
|
|
144
|
+
borderTopStyle: 'solid',
|
|
145
|
+
borderTopColor: convertBorderValue(borderTop, theme.border.color),
|
|
146
|
+
borderTopWidth: theme.utils.mapResponsiveScale(borderWidth, theme.border.width)
|
|
147
|
+
} : {}), borderBottom ? {
|
|
148
|
+
borderBottomStyle: 'solid',
|
|
149
|
+
borderBottomColor: convertBorderValue(borderBottom, theme.border.color),
|
|
150
|
+
borderBottomWidth: theme.utils.mapResponsiveScale(borderWidth, theme.border.width)
|
|
151
|
+
} : {}), borderLeft ? {
|
|
152
|
+
borderLeftStyle: 'solid',
|
|
153
|
+
borderLeftColor: convertBorderValue(borderLeft, theme.border.color),
|
|
154
|
+
borderLeftWidth: theme.utils.mapResponsiveScale(borderWidth, theme.border.width)
|
|
155
|
+
} : {}), borderRight ? {
|
|
156
|
+
borderRightStyle: 'solid',
|
|
157
|
+
borderRightColor: convertBorderValue(borderRight, theme.border.color),
|
|
158
|
+
borderRightWidth: theme.utils.mapResponsiveScale(borderWidth, theme.border.width)
|
|
159
|
+
} : {});
|
|
160
|
+
return theme.utils.resolveResponsiveProps(_objectSpread(_objectSpread(_objectSpread({}, unresponsiveProps), conditionalBorderStyles), {}, {
|
|
161
|
+
// allow padding and height/width props to play nice
|
|
162
|
+
display: theme.utils.mapResponsiveProp(display),
|
|
163
|
+
// margin
|
|
164
|
+
marginBottom: theme.utils.mapResponsiveScale(marginBottom || marginY || margin, theme.spacing),
|
|
165
|
+
marginTop: theme.utils.mapResponsiveScale(marginTop || marginY || margin, theme.spacing),
|
|
166
|
+
marginLeft: theme.utils.mapResponsiveScale(marginLeft || marginX || margin, theme.spacing),
|
|
167
|
+
marginRight: theme.utils.mapResponsiveScale(marginRight || marginX || margin, theme.spacing),
|
|
168
|
+
// padding
|
|
169
|
+
paddingBottom: theme.utils.mapResponsiveScale(paddingBottom || paddingY || padding, theme.spacing),
|
|
170
|
+
paddingTop: theme.utils.mapResponsiveScale(paddingTop || paddingY || padding, theme.spacing),
|
|
171
|
+
paddingLeft: theme.utils.mapResponsiveScale(paddingLeft || paddingX || padding, theme.spacing),
|
|
172
|
+
paddingRight: theme.utils.mapResponsiveScale(paddingRight || paddingX || padding, theme.spacing),
|
|
173
|
+
// border
|
|
174
|
+
borderRadius: typeof borderRadius === 'number' ? borderRadius : theme.utils.mapResponsiveScale(borderRadius, theme.border.radius),
|
|
175
|
+
// flex: parent
|
|
176
|
+
alignItems: theme.utils.mapResponsiveScale(alignItems, flexMap.alignItems),
|
|
177
|
+
gap: theme.utils.mapResponsiveScale(gap, theme.spacing),
|
|
178
|
+
flexDirection: theme.utils.mapResponsiveScale(flexDirection, flexMap.flexDirection),
|
|
179
|
+
justifyContent: theme.utils.mapResponsiveScale(justifyContent, flexMap.justifyContent),
|
|
180
|
+
flexWrap: theme.utils.mapResponsiveProp(flexWrap),
|
|
181
|
+
// flex: child
|
|
182
|
+
alignSelf: theme.utils.mapResponsiveScale(alignSelf, flexMap.alignItems),
|
|
183
|
+
flex: theme.utils.mapResponsiveProp(flex),
|
|
184
|
+
flexGrow: theme.utils.mapResponsiveProp(flexGrow),
|
|
185
|
+
flexShrink: theme.utils.mapResponsiveProp(flexShrink),
|
|
186
|
+
// dimension
|
|
187
|
+
height: theme.utils.mapResponsiveScale(height, theme.sizing),
|
|
188
|
+
width: theme.utils.mapResponsiveScale(width, theme.sizing),
|
|
189
|
+
// position
|
|
190
|
+
position: theme.utils.mapResponsiveProp(position),
|
|
191
|
+
bottom: theme.utils.mapResponsiveProp(bottom),
|
|
192
|
+
left: theme.utils.mapResponsiveProp(left),
|
|
193
|
+
right: theme.utils.mapResponsiveProp(right),
|
|
194
|
+
top: theme.utils.mapResponsiveProp(top),
|
|
195
|
+
zIndex: theme.utils.mapResponsiveScale(zIndex, theme.elevation)
|
|
196
|
+
}));
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
// Flex shorthand / adjustments
|
|
200
|
+
// ------------------------------
|
|
201
|
+
|
|
202
|
+
var flexMap = {
|
|
203
|
+
alignItems: {
|
|
204
|
+
start: 'flex-start',
|
|
205
|
+
center: 'center',
|
|
206
|
+
end: 'flex-end',
|
|
207
|
+
stretch: 'stretch'
|
|
208
|
+
},
|
|
209
|
+
justifyContent: {
|
|
210
|
+
start: 'flex-start',
|
|
211
|
+
center: 'center',
|
|
212
|
+
end: 'flex-end',
|
|
213
|
+
spaceBetween: 'space-between',
|
|
214
|
+
stretch: 'stretch'
|
|
215
|
+
},
|
|
216
|
+
flexDirection: {
|
|
217
|
+
row: 'row',
|
|
218
|
+
rowReverse: 'row-reverse',
|
|
219
|
+
column: 'column',
|
|
220
|
+
columnReverse: 'column-reverse'
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
var _excluded$1 = ["alignItems", "alignSelf", "background", "border", "borderRadius", "borderWidth", "borderTop", "borderBottom", "borderLeft", "borderRight", "bottom", "cursor", "display", "flex", "flexDirection", "flexGrow", "flexShrink", "flexWrap", "gap", "height", "justifyContent", "left", "margin", "marginBottom", "marginLeft", "marginRight", "marginTop", "marginX", "marginY", "minHeight", "minWidth", "opacity", "overflow", "padding", "paddingBottom", "paddingLeft", "paddingRight", "paddingTop", "paddingX", "paddingY", "position", "right", "shadow", "top", "userSelect", "width", "zIndex"];
|
|
225
|
+
|
|
226
|
+
/** Separate the style properties from the element attributes. */
|
|
227
|
+
function useBoxProps(props) {
|
|
228
|
+
var alignItems = props.alignItems,
|
|
229
|
+
alignSelf = props.alignSelf,
|
|
230
|
+
background = props.background,
|
|
231
|
+
border = props.border,
|
|
232
|
+
borderRadius = props.borderRadius,
|
|
233
|
+
borderWidth = props.borderWidth,
|
|
234
|
+
borderTop = props.borderTop,
|
|
235
|
+
borderBottom = props.borderBottom,
|
|
236
|
+
borderLeft = props.borderLeft,
|
|
237
|
+
borderRight = props.borderRight,
|
|
238
|
+
bottom = props.bottom,
|
|
239
|
+
cursor = props.cursor,
|
|
240
|
+
display = props.display,
|
|
241
|
+
flex = props.flex,
|
|
242
|
+
flexDirection = props.flexDirection,
|
|
243
|
+
flexGrow = props.flexGrow,
|
|
244
|
+
flexShrink = props.flexShrink,
|
|
245
|
+
flexWrap = props.flexWrap,
|
|
246
|
+
gap = props.gap,
|
|
247
|
+
height = props.height,
|
|
248
|
+
justifyContent = props.justifyContent,
|
|
249
|
+
left = props.left,
|
|
250
|
+
margin = props.margin,
|
|
251
|
+
marginBottom = props.marginBottom,
|
|
252
|
+
marginLeft = props.marginLeft,
|
|
253
|
+
marginRight = props.marginRight,
|
|
254
|
+
marginTop = props.marginTop,
|
|
255
|
+
marginX = props.marginX,
|
|
256
|
+
marginY = props.marginY,
|
|
257
|
+
minHeight = props.minHeight,
|
|
258
|
+
minWidth = props.minWidth,
|
|
259
|
+
opacity = props.opacity,
|
|
260
|
+
overflow = props.overflow,
|
|
261
|
+
padding = props.padding,
|
|
262
|
+
paddingBottom = props.paddingBottom,
|
|
263
|
+
paddingLeft = props.paddingLeft,
|
|
264
|
+
paddingRight = props.paddingRight,
|
|
265
|
+
paddingTop = props.paddingTop,
|
|
266
|
+
paddingX = props.paddingX,
|
|
267
|
+
paddingY = props.paddingY,
|
|
268
|
+
position = props.position,
|
|
269
|
+
right = props.right,
|
|
270
|
+
shadow = props.shadow,
|
|
271
|
+
top = props.top,
|
|
272
|
+
userSelect = props.userSelect,
|
|
273
|
+
width = props.width,
|
|
274
|
+
zIndex = props.zIndex,
|
|
275
|
+
attributes = _objectWithoutProperties(props, _excluded$1);
|
|
276
|
+
var styles = useBoxStyles({
|
|
277
|
+
alignItems: alignItems,
|
|
278
|
+
alignSelf: alignSelf,
|
|
279
|
+
background: background,
|
|
280
|
+
border: border,
|
|
281
|
+
borderRadius: borderRadius,
|
|
282
|
+
borderWidth: borderWidth,
|
|
283
|
+
borderBottom: borderBottom,
|
|
284
|
+
borderLeft: borderLeft,
|
|
285
|
+
borderRight: borderRight,
|
|
286
|
+
borderTop: borderTop,
|
|
287
|
+
bottom: bottom,
|
|
288
|
+
cursor: cursor,
|
|
289
|
+
display: display,
|
|
290
|
+
flex: flex,
|
|
291
|
+
flexDirection: flexDirection,
|
|
292
|
+
flexGrow: flexGrow,
|
|
293
|
+
flexShrink: flexShrink,
|
|
294
|
+
flexWrap: flexWrap,
|
|
295
|
+
gap: gap,
|
|
296
|
+
height: height,
|
|
297
|
+
justifyContent: justifyContent,
|
|
298
|
+
left: left,
|
|
299
|
+
margin: margin,
|
|
300
|
+
marginBottom: marginBottom,
|
|
301
|
+
marginLeft: marginLeft,
|
|
302
|
+
marginRight: marginRight,
|
|
303
|
+
marginTop: marginTop,
|
|
304
|
+
marginX: marginX,
|
|
305
|
+
marginY: marginY,
|
|
306
|
+
minHeight: minHeight,
|
|
307
|
+
minWidth: minWidth,
|
|
308
|
+
opacity: opacity,
|
|
309
|
+
overflow: overflow,
|
|
310
|
+
padding: padding,
|
|
311
|
+
paddingBottom: paddingBottom,
|
|
312
|
+
paddingLeft: paddingLeft,
|
|
313
|
+
paddingRight: paddingRight,
|
|
314
|
+
paddingTop: paddingTop,
|
|
315
|
+
paddingX: paddingX,
|
|
316
|
+
paddingY: paddingY,
|
|
317
|
+
position: position,
|
|
318
|
+
right: right,
|
|
319
|
+
shadow: shadow,
|
|
320
|
+
top: top,
|
|
321
|
+
userSelect: userSelect,
|
|
322
|
+
width: width,
|
|
323
|
+
zIndex: zIndex
|
|
324
|
+
});
|
|
325
|
+
return {
|
|
326
|
+
styles: styles,
|
|
327
|
+
attributes: attributes
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
var _excluded = ["as", "asElement", "css", "data", "id"];
|
|
332
|
+
/** Exposes a prop-based API for adding styles to a view, within the constraints of the theme. */
|
|
333
|
+
var Box = forwardRefWithAs(function (_ref, forwardedRef) {
|
|
334
|
+
var _ref$as = _ref.as,
|
|
335
|
+
Tag = _ref$as === void 0 ? 'div' : _ref$as,
|
|
336
|
+
asElement = _ref.asElement,
|
|
337
|
+
cssProps = _ref.css,
|
|
338
|
+
data = _ref.data,
|
|
339
|
+
id = _ref.id,
|
|
340
|
+
props = _objectWithoutProperties(_ref, _excluded);
|
|
341
|
+
var _useBoxProps = useBoxProps(props),
|
|
342
|
+
styles = _useBoxProps.styles,
|
|
343
|
+
attributes = _useBoxProps.attributes;
|
|
344
|
+
var resetStyles = resetElementStyles(asElement !== null && asElement !== void 0 ? asElement : Tag);
|
|
345
|
+
var element = jsx(Tag, _objectSpread(_objectSpread({}, data ? buildDataAttributes(data) : undefined), {}, {
|
|
346
|
+
ref: forwardedRef,
|
|
347
|
+
id: id,
|
|
348
|
+
css: [css(resetStyles), css(styles), cssProps]
|
|
349
|
+
}, attributes));
|
|
350
|
+
return renderBackgroundProvider(props.background, element);
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
export { BackgroundProvider, Box, useBackground, useBackgroundLightness };
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@spark-web/box",
|
|
3
|
+
"version": "0.0.0-snapshot-release-20260409001813",
|
|
4
|
+
"homepage": "https://github.com/brighte-labs/spark-web#readme",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/brighte-labs/spark-web.git",
|
|
8
|
+
"directory": "packages/box"
|
|
9
|
+
},
|
|
10
|
+
"main": "dist/spark-web-box.cjs.js",
|
|
11
|
+
"module": "dist/spark-web-box.esm.js",
|
|
12
|
+
"files": [
|
|
13
|
+
"CHANGELOG.md",
|
|
14
|
+
"CLAUDE.md",
|
|
15
|
+
"dist",
|
|
16
|
+
"README.md"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@babel/runtime": "^7.25.0",
|
|
20
|
+
"@emotion/react": "^11.14.0",
|
|
21
|
+
"@spark-web/utils": "^5.1.0"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@spark-web/theme": "^5.13.0",
|
|
25
|
+
"@types/react": "^19.1.0",
|
|
26
|
+
"react": "^19.1.0"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"@spark-web/theme": "^5.13.0",
|
|
30
|
+
"react": "^17.0.0 || ^18.0.0 || ^19.0.0"
|
|
31
|
+
},
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=14"
|
|
34
|
+
}
|
|
35
|
+
}
|