@spark-web/text 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,278 @@
1
+ import { createContext, useContext, useMemo } from 'react';
2
+ import { jsx } from '@emotion/react/jsx-runtime';
3
+ import { css } from '@emotion/react';
4
+ import { useTheme } from '@spark-web/theme';
5
+ import _objectSpread from '@babel/runtime/helpers/esm/objectSpread2';
6
+ import _objectWithoutProperties from '@babel/runtime/helpers/esm/objectWithoutProperties';
7
+ import { useBackgroundLightness, Box } from '@spark-web/box';
8
+ import { forwardRefWithAs } from '@spark-web/utils/ts';
9
+
10
+ var TextContext = /*#__PURE__*/createContext(undefined);
11
+ function useTextContext() {
12
+ return useContext(TextContext);
13
+ }
14
+
15
+ var DefaultTextPropsContext = /*#__PURE__*/createContext({
16
+ size: undefined,
17
+ tone: undefined,
18
+ weight: undefined
19
+ });
20
+ function DefaultTextPropsProvider(_ref) {
21
+ var children = _ref.children,
22
+ size = _ref.size,
23
+ tone = _ref.tone,
24
+ weight = _ref.weight;
25
+ var defaultTextProps = useMemo(function () {
26
+ return {
27
+ size: size,
28
+ tone: tone,
29
+ weight: weight
30
+ };
31
+ }, [size, tone, weight]);
32
+ return jsx(DefaultTextPropsContext.Provider, {
33
+ value: defaultTextProps,
34
+ children: children
35
+ });
36
+ }
37
+ var useDefaultTextProps = function useDefaultTextProps(_ref2) {
38
+ var _ref3, _ref4;
39
+ var sizeProp = _ref2.size,
40
+ toneProp = _ref2.tone,
41
+ weightProp = _ref2.weight;
42
+ var _useContext = useContext(DefaultTextPropsContext),
43
+ size = _useContext.size,
44
+ tone = _useContext.tone,
45
+ weight = _useContext.weight;
46
+ return {
47
+ size: (_ref3 = sizeProp !== null && sizeProp !== void 0 ? sizeProp : size) !== null && _ref3 !== void 0 ? _ref3 : 'standard',
48
+ tone: (_ref4 = toneProp !== null && toneProp !== void 0 ? toneProp : tone) !== null && _ref4 !== void 0 ? _ref4 : 'neutral',
49
+ weight: weightProp !== null && weightProp !== void 0 ? weightProp : weight
50
+ };
51
+ };
52
+
53
+ var Strong = function Strong(_ref) {
54
+ var children = _ref.children;
55
+ var theme = useTheme();
56
+ var styles = {
57
+ fontWeight: theme.typography.fontWeight.semibold
58
+ };
59
+ return jsx("strong", {
60
+ css: css(styles),
61
+ children: children
62
+ });
63
+ };
64
+
65
+ var useNumberOfLines = function useNumberOfLines(numberOfLines) {
66
+ if (!numberOfLines) {
67
+ return {};
68
+ }
69
+ return {
70
+ overflow: 'hidden',
71
+ display: '-webkit-box',
72
+ WebkitLineClamp: numberOfLines,
73
+ lineClamp: numberOfLines,
74
+ WebkitBoxOrient: 'vertical',
75
+ textOverflow: 'ellipsis',
76
+ overflowWrap: 'break-word',
77
+ wordBreak: 'break-word',
78
+ wordWrap: 'break-word'
79
+ };
80
+ };
81
+
82
+ var strategyMap = {
83
+ truncate: {
84
+ display: 'block',
85
+ overflow: 'hidden',
86
+ textOverflow: 'ellipsis',
87
+ whiteSpace: 'nowrap'
88
+ },
89
+ nowrap: {
90
+ whiteSpace: 'nowrap'
91
+ },
92
+ // https://css-tricks.com/better-line-breaks-for-long-urls/
93
+ breakword: {
94
+ display: 'block',
95
+ overflowWrap: 'break-word',
96
+ wordBreak: 'break-word',
97
+ wordWrap: 'break-word'
98
+ }
99
+ };
100
+ function useOverflowStrategy(strategy) {
101
+ if (!strategy) {
102
+ return null;
103
+ }
104
+ return strategyMap[strategy];
105
+ }
106
+
107
+ var invertableTones = {
108
+ neutral: {
109
+ dark: 'neutralInverted',
110
+ light: 'neutral'
111
+ },
112
+ muted: {
113
+ dark: 'mutedInverted',
114
+ light: 'muted'
115
+ },
116
+ link: {
117
+ dark: 'neutralInverted',
118
+ light: 'link'
119
+ }
120
+ };
121
+ function useForegroundTone(tone) {
122
+ var theme = useTheme();
123
+ var backgroundLightness = useBackgroundLightness();
124
+ if (tone in invertableTones) {
125
+ return theme.color.foreground[invertableTones[tone][backgroundLightness]];
126
+ }
127
+ return theme.color.foreground[tone];
128
+ }
129
+
130
+ function useText(_ref) {
131
+ var _ref$baseline = _ref.baseline,
132
+ baseline = _ref$baseline === void 0 ? true : _ref$baseline,
133
+ size = _ref.size,
134
+ tone = _ref.tone,
135
+ weight = _ref.weight;
136
+ var theme = useTheme();
137
+ var color = useForegroundTone(tone);
138
+ var _theme$typography$tex = theme.typography.text[size],
139
+ mobile = _theme$typography$tex.mobile,
140
+ tablet = _theme$typography$tex.tablet;
141
+ var responsiveStyles = theme.utils.responsiveStyles({
142
+ mobile: createTextStyles(mobile, {
143
+ includeTrims: baseline,
144
+ includeWeight: !weight
145
+ }),
146
+ tablet: createTextStyles(tablet, {
147
+ includeTrims: baseline,
148
+ includeWeight: !weight
149
+ })
150
+ });
151
+ var styles = [responsiveStyles, {
152
+ color: color,
153
+ fontFamily: theme.typography.fontFamily.sans.name,
154
+ fontWeight: weight ? theme.typography.fontWeight[weight] : undefined
155
+ }];
156
+ return styles;
157
+ }
158
+ function createTextStyles(_ref2) {
159
+ var fontSize = _ref2.fontSize,
160
+ fontWeight = _ref2.fontWeight,
161
+ lineHeight = _ref2.lineHeight,
162
+ trims = _ref2.trims;
163
+ var _ref3 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
164
+ _ref3$includeTrims = _ref3.includeTrims,
165
+ includeTrims = _ref3$includeTrims === void 0 ? true : _ref3$includeTrims,
166
+ _ref3$includeWeight = _ref3.includeWeight,
167
+ includeWeight = _ref3$includeWeight === void 0 ? true : _ref3$includeWeight;
168
+ var pseudo = {
169
+ content: '" "',
170
+ display: 'table'
171
+ };
172
+ var leadingTrim = includeTrims ? {
173
+ '::before': _objectSpread(_objectSpread({}, pseudo), {}, {
174
+ marginBottom: trims.capHeightTrim
175
+ }),
176
+ '::after': _objectSpread(_objectSpread({}, pseudo), {}, {
177
+ marginTop: trims.baselineTrim
178
+ })
179
+ } : null;
180
+ return _objectSpread({
181
+ fontSize: fontSize,
182
+ fontWeight: includeWeight ? fontWeight : undefined,
183
+ lineHeight: lineHeight
184
+ }, leadingTrim);
185
+ }
186
+
187
+ var _excluded = ["as", "children", "data", "id", "align", "baseline", "css", "inline", "overflowStrategy", "size", "tabularNumbers", "tone", "transform", "weight", "numberOfLines"];
188
+ var Text = forwardRefWithAs(function (_ref, forwardedRef) {
189
+ var as = _ref.as,
190
+ children = _ref.children,
191
+ data = _ref.data,
192
+ id = _ref.id,
193
+ align = _ref.align,
194
+ baselineProp = _ref.baseline,
195
+ cssProps = _ref.css,
196
+ inline = _ref.inline,
197
+ overflowStrategy = _ref.overflowStrategy,
198
+ sizeProp = _ref.size,
199
+ tabularNumbers = _ref.tabularNumbers,
200
+ toneProp = _ref.tone,
201
+ transform = _ref.transform,
202
+ weightProp = _ref.weight,
203
+ numberOfLines = _ref.numberOfLines,
204
+ consumerProps = _objectWithoutProperties(_ref, _excluded);
205
+ var overflowStyles = useOverflowStrategy(overflowStrategy);
206
+ var textContext = useTextContext();
207
+ var _useDefaultTextProps = useDefaultTextProps({
208
+ size: sizeProp !== null && sizeProp !== void 0 ? sizeProp : textContext === null || textContext === void 0 ? void 0 : textContext.size,
209
+ tone: toneProp !== null && toneProp !== void 0 ? toneProp : textContext === null || textContext === void 0 ? void 0 : textContext.tone,
210
+ weight: weightProp !== null && weightProp !== void 0 ? weightProp : textContext === null || textContext === void 0 ? void 0 : textContext.weight
211
+ }),
212
+ size = _useDefaultTextProps.size,
213
+ tone = _useDefaultTextProps.tone,
214
+ weight = _useDefaultTextProps.weight;
215
+ var baseline = !inline && baselineProp;
216
+ var textStyles = useText({
217
+ baseline: baseline,
218
+ size: size,
219
+ tone: tone,
220
+ weight: weight
221
+ });
222
+ var numberOfLinesStyle = useNumberOfLines(numberOfLines);
223
+ var styles = [textStyles, {
224
+ display: inline ? 'inline' : 'block',
225
+ fontVariantNumeric: tabularNumbers ? 'tabular-nums' : undefined,
226
+ textAlign: align,
227
+ textTransform: transform
228
+ }];
229
+
230
+ // early exit for inline variant
231
+ if (inline) {
232
+ return jsx(Box, _objectSpread(_objectSpread({}, consumerProps), {}, {
233
+ as: as !== null && as !== void 0 ? as : 'span',
234
+ data: data,
235
+ ref: forwardedRef,
236
+ id: id,
237
+ css: css(styles, cssProps),
238
+ children: children
239
+ }));
240
+ }
241
+
242
+ // prepare block variant
243
+ var content = function content() {
244
+ if (overflowStrategy) {
245
+ return jsx("span", {
246
+ css: css(overflowStyles),
247
+ children: children
248
+ });
249
+ }
250
+ if (numberOfLines) {
251
+ return jsx("span", {
252
+ style: numberOfLinesStyle,
253
+ children: children
254
+ });
255
+ }
256
+ return children;
257
+ };
258
+ var textContextValue = useMemo(function () {
259
+ return {
260
+ size: size,
261
+ tone: tone,
262
+ weight: weight
263
+ };
264
+ }, [size, tone, weight]);
265
+ return jsx(TextContext.Provider, {
266
+ value: textContextValue,
267
+ children: jsx(Box, _objectSpread(_objectSpread({}, consumerProps), {}, {
268
+ as: as,
269
+ data: data,
270
+ ref: forwardedRef,
271
+ id: id,
272
+ css: css(styles, cssProps),
273
+ children: content()
274
+ }))
275
+ });
276
+ });
277
+
278
+ export { DefaultTextPropsProvider, Strong, Text, createTextStyles, useDefaultTextProps, useForegroundTone, useNumberOfLines, useOverflowStrategy, useText, useTextContext };
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@spark-web/text",
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/text"
9
+ },
10
+ "main": "dist/spark-web-text.cjs.js",
11
+ "module": "dist/spark-web-text.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/box": "0.0.0-snapshot-release-20260409001813",
22
+ "@spark-web/theme": "^5.13.0",
23
+ "@spark-web/utils": "^5.1.0"
24
+ },
25
+ "devDependencies": {
26
+ "@types/react": "^19.1.0",
27
+ "react": "^19.1.0"
28
+ },
29
+ "peerDependencies": {
30
+ "react": "^17.0.0 || ^18.0.0 || ^19.0.0"
31
+ },
32
+ "engines": {
33
+ "node": ">=14"
34
+ }
35
+ }