UNPKG

12.8 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5var theme = require('@spark-web/theme');
6var react = require('react');
7var jsxRuntime = require('react/jsx-runtime');
8var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
9var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
10var css = require('@emotion/css');
11var internal = require('@spark-web/utils/internal');
12var ts = require('@spark-web/utils/ts');
13
14// prepare context
15
16var backgroundContext = /*#__PURE__*/react.createContext('body');
17var InternalBackgroundProvider = backgroundContext.Provider;
18var useBackground = function useBackground() {
19 return react.useContext(backgroundContext);
20};
21
22// conditional provider
23
24function renderBackgroundProvider(background, element) {
25 return background ? /*#__PURE__*/jsxRuntime.jsx(InternalBackgroundProvider, {
26 value: background,
27 children: element
28 }) : element;
29}
30
31// a11y contrast utility
32
33var 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. */
50var 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// 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
64var 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$1 = theme.useTheme();
114 var unresponsiveProps = {
115 background: background ? theme$1.color.background[background] : undefined,
116 boxShadow: shadow ? theme$1.shadow[shadow] : undefined,
117 cursor: cursor,
118 minHeight: minHeight,
119 minWidth: minWidth,
120 opacity: opacity,
121 overflow: overflow,
122 userSelect: userSelect
123 };
124 var conditionalBorderStyles = _objectSpread(_objectSpread(_objectSpread(_objectSpread(_objectSpread({}, border ? {
125 borderStyle: 'solid',
126 borderColor: theme$1.utils.mapResponsiveScale(border, theme$1.border.color),
127 borderWidth: theme$1.utils.mapResponsiveScale(borderWidth, theme$1.border.width)
128 } : {}), borderTop ? {
129 borderTopStyle: 'solid',
130 borderTopColor: theme$1.utils.mapResponsiveScale(borderTop, theme$1.border.color),
131 borderTopWidth: theme$1.utils.mapResponsiveScale(borderWidth, theme$1.border.width)
132 } : {}), borderBottom ? {
133 borderBottomStyle: 'solid',
134 borderBottomColor: theme$1.utils.mapResponsiveScale(borderBottom, theme$1.border.color),
135 borderBottomWidth: theme$1.utils.mapResponsiveScale(borderWidth, theme$1.border.width)
136 } : {}), borderLeft ? {
137 borderLeftStyle: 'solid',
138 borderLeftColor: theme$1.utils.mapResponsiveScale(borderLeft, theme$1.border.color),
139 borderLeftWidth: theme$1.utils.mapResponsiveScale(borderWidth, theme$1.border.width)
140 } : {}), borderRight ? {
141 borderRightStyle: 'solid',
142 borderRightColor: theme$1.utils.mapResponsiveScale(borderRight, theme$1.border.color),
143 borderRightWidth: theme$1.utils.mapResponsiveScale(borderWidth, theme$1.border.width)
144 } : {});
145 return theme$1.utils.resolveResponsiveProps(_objectSpread(_objectSpread(_objectSpread({}, unresponsiveProps), conditionalBorderStyles), {}, {
146 // allow padding and height/width props to play nice
147 display: theme$1.utils.mapResponsiveProp(display),
148 // margin
149 marginBottom: theme$1.utils.mapResponsiveScale(marginBottom || marginY || margin, theme$1.spacing),
150 marginTop: theme$1.utils.mapResponsiveScale(marginTop || marginY || margin, theme$1.spacing),
151 marginLeft: theme$1.utils.mapResponsiveScale(marginLeft || marginX || margin, theme$1.spacing),
152 marginRight: theme$1.utils.mapResponsiveScale(marginRight || marginX || margin, theme$1.spacing),
153 // padding
154 paddingBottom: theme$1.utils.mapResponsiveScale(paddingBottom || paddingY || padding, theme$1.spacing),
155 paddingTop: theme$1.utils.mapResponsiveScale(paddingTop || paddingY || padding, theme$1.spacing),
156 paddingLeft: theme$1.utils.mapResponsiveScale(paddingLeft || paddingX || padding, theme$1.spacing),
157 paddingRight: theme$1.utils.mapResponsiveScale(paddingRight || paddingX || padding, theme$1.spacing),
158 // border
159 borderRadius: theme$1.utils.mapResponsiveScale(borderRadius, theme$1.border.radius),
160 // flex: parent
161 alignItems: theme$1.utils.mapResponsiveScale(alignItems, flexMap.alignItems),
162 gap: theme$1.utils.mapResponsiveScale(gap, theme$1.spacing),
163 flexDirection: theme$1.utils.mapResponsiveScale(flexDirection, flexMap.flexDirection),
164 justifyContent: theme$1.utils.mapResponsiveScale(justifyContent, flexMap.justifyContent),
165 flexWrap: theme$1.utils.mapResponsiveProp(flexWrap),
166 // flex: child
167 alignSelf: theme$1.utils.mapResponsiveScale(alignSelf, flexMap.alignItems),
168 flex: theme$1.utils.mapResponsiveProp(flex),
169 flexGrow: theme$1.utils.mapResponsiveProp(flexGrow),
170 flexShrink: theme$1.utils.mapResponsiveProp(flexShrink),
171 // dimension
172 height: theme$1.utils.mapResponsiveScale(height, theme$1.sizing),
173 width: theme$1.utils.mapResponsiveScale(width, theme$1.sizing),
174 // position
175 position: theme$1.utils.mapResponsiveProp(position),
176 bottom: theme$1.utils.mapResponsiveProp(bottom),
177 left: theme$1.utils.mapResponsiveProp(left),
178 right: theme$1.utils.mapResponsiveProp(right),
179 top: theme$1.utils.mapResponsiveProp(top),
180 zIndex: theme$1.utils.mapResponsiveScale(zIndex, theme$1.elevation)
181 }));
182};
183
184// Flex shorthand / adjustments
185// ------------------------------
186
187var flexMap = {
188 alignItems: {
189 start: 'flex-start',
190 center: 'center',
191 end: 'flex-end',
192 stretch: 'stretch'
193 },
194 justifyContent: {
195 start: 'flex-start',
196 center: 'center',
197 end: 'flex-end',
198 spaceBetween: 'space-between',
199 stretch: 'stretch'
200 },
201 flexDirection: {
202 row: 'row',
203 rowReverse: 'row-reverse',
204 column: 'column',
205 columnReverse: 'column-reverse'
206 }
207};
208
209var _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"];
210
211/** Separate the style properties from the element attributes. */
212function useBoxProps(props) {
213 var alignItems = props.alignItems,
214 alignSelf = props.alignSelf,
215 background = props.background,
216 border = props.border,
217 borderRadius = props.borderRadius,
218 borderWidth = props.borderWidth,
219 borderTop = props.borderTop,
220 borderBottom = props.borderBottom,
221 borderLeft = props.borderLeft,
222 borderRight = props.borderRight,
223 bottom = props.bottom,
224 cursor = props.cursor,
225 display = props.display,
226 flex = props.flex,
227 flexDirection = props.flexDirection,
228 flexGrow = props.flexGrow,
229 flexShrink = props.flexShrink,
230 flexWrap = props.flexWrap,
231 gap = props.gap,
232 height = props.height,
233 justifyContent = props.justifyContent,
234 left = props.left,
235 margin = props.margin,
236 marginBottom = props.marginBottom,
237 marginLeft = props.marginLeft,
238 marginRight = props.marginRight,
239 marginTop = props.marginTop,
240 marginX = props.marginX,
241 marginY = props.marginY,
242 minHeight = props.minHeight,
243 minWidth = props.minWidth,
244 opacity = props.opacity,
245 overflow = props.overflow,
246 padding = props.padding,
247 paddingBottom = props.paddingBottom,
248 paddingLeft = props.paddingLeft,
249 paddingRight = props.paddingRight,
250 paddingTop = props.paddingTop,
251 paddingX = props.paddingX,
252 paddingY = props.paddingY,
253 position = props.position,
254 right = props.right,
255 shadow = props.shadow,
256 top = props.top,
257 userSelect = props.userSelect,
258 width = props.width,
259 zIndex = props.zIndex,
260 attributes = _objectWithoutProperties(props, _excluded$1);
261 var styles = useBoxStyles({
262 alignItems: alignItems,
263 alignSelf: alignSelf,
264 background: background,
265 border: border,
266 borderRadius: borderRadius,
267 borderWidth: borderWidth,
268 borderBottom: borderBottom,
269 borderLeft: borderLeft,
270 borderRight: borderRight,
271 borderTop: borderTop,
272 bottom: bottom,
273 cursor: cursor,
274 display: display,
275 flex: flex,
276 flexDirection: flexDirection,
277 flexGrow: flexGrow,
278 flexShrink: flexShrink,
279 flexWrap: flexWrap,
280 gap: gap,
281 height: height,
282 justifyContent: justifyContent,
283 left: left,
284 margin: margin,
285 marginBottom: marginBottom,
286 marginLeft: marginLeft,
287 marginRight: marginRight,
288 marginTop: marginTop,
289 marginX: marginX,
290 marginY: marginY,
291 minHeight: minHeight,
292 minWidth: minWidth,
293 opacity: opacity,
294 overflow: overflow,
295 padding: padding,
296 paddingBottom: paddingBottom,
297 paddingLeft: paddingLeft,
298 paddingRight: paddingRight,
299 paddingTop: paddingTop,
300 paddingX: paddingX,
301 paddingY: paddingY,
302 position: position,
303 right: right,
304 shadow: shadow,
305 top: top,
306 userSelect: userSelect,
307 width: width,
308 zIndex: zIndex
309 });
310 return {
311 styles: styles,
312 attributes: attributes
313 };
314}
315
316var _excluded = ["as", "asElement", "className", "data", "id"];
317/** Exposes a prop-based API for adding styles to a view, within the constraints of the theme. */
318var Box = ts.forwardRefWithAs(function (_ref, forwardedRef) {
319 var _ref$as = _ref.as,
320 Tag = _ref$as === void 0 ? 'div' : _ref$as,
321 asElement = _ref.asElement,
322 className = _ref.className,
323 data = _ref.data,
324 id = _ref.id,
325 props = _objectWithoutProperties(_ref, _excluded);
326 var _useBoxProps = useBoxProps(props),
327 styles = _useBoxProps.styles,
328 attributes = _useBoxProps.attributes;
329 var resetStyles = internal.resetElementStyles(asElement !== null && asElement !== void 0 ? asElement : Tag);
330 var element = /*#__PURE__*/jsxRuntime.jsx(Tag, _objectSpread(_objectSpread({}, data ? internal.buildDataAttributes(data) : undefined), {}, {
331 ref: forwardedRef,
332 id: id,
333 className: css.cx(css.css(resetStyles), css.css(styles), className)
334 }, attributes));
335 return renderBackgroundProvider(props.background, element);
336});
337
338exports.BackgroundProvider = BackgroundProvider;
339exports.Box = Box;
340exports.useBackground = useBackground;
341exports.useBackgroundLightness = useBackgroundLightness;