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