@spark-web/field 3.0.6 → 4.0.0-rc.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/CHANGELOG.md CHANGED
@@ -1,5 +1,43 @@
1
1
  # @spark-web/field
2
2
 
3
+ ## 4.0.0-rc.0
4
+
5
+ ### Major Changes
6
+
7
+ - rc versio
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies []:
12
+ - @spark-web/stack@2.0.0-rc.0
13
+ - @spark-web/theme@4.0.0-rc.0
14
+ - @spark-web/a11y@2.0.0-rc.0
15
+ - @spark-web/icon@2.0.0-rc.0
16
+ - @spark-web/text@2.0.0-rc.0
17
+ - @spark-web/box@2.0.0-rc.0
18
+ - @spark-web/utils@2.0.0-rc.0
19
+
20
+ ## 3.1.0
21
+
22
+ ### Minor Changes
23
+
24
+ - [#382](https://github.com/brighte-labs/spark-web/pull/382)
25
+ [`955bf5d`](https://github.com/brighte-labs/spark-web/commit/955bf5d7698bfdf45e7f317aa3e726c81d3444c0)
26
+ Thanks [@dilipt-brighte](https://github.com/dilipt-brighte)! - Updates React
27
+ version to latest (18.2.0)
28
+
29
+ ### Patch Changes
30
+
31
+ - Updated dependencies
32
+ [[`955bf5d`](https://github.com/brighte-labs/spark-web/commit/955bf5d7698bfdf45e7f317aa3e726c81d3444c0)]:
33
+ - @spark-web/a11y@1.4.0
34
+ - @spark-web/box@1.2.0
35
+ - @spark-web/icon@1.3.0
36
+ - @spark-web/stack@1.1.0
37
+ - @spark-web/text@1.2.0
38
+ - @spark-web/theme@3.2.0
39
+ - @spark-web/utils@1.3.0
40
+
3
41
  ## 3.0.6
4
42
 
5
43
  ### Patch Changes
@@ -1 +1,2 @@
1
- export * from "./declarations/src/index";
1
+ export * from "../src/index";
2
+ //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic3Bhcmstd2ViLWZpZWxkLmNqcy5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vc3JjL2luZGV4LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBIn0=
@@ -1,7 +1,16 @@
1
- 'use strict';
1
+ "use strict";
2
+ // this file might look strange and you might be wondering what it's for
3
+ // it's lets you import your source files by importing this entrypoint
4
+ // as you would import it if it was built with preconstruct build
5
+ // this file is slightly different to some others though
6
+ // it has a require hook which compiles your code with Babel
7
+ // this means that you don't have to set up @babel/register or anything like that
8
+ // but you can still require this module and it'll be compiled
2
9
 
3
- if (process.env.NODE_ENV === "production") {
4
- module.exports = require("./spark-web-field.cjs.prod.js");
5
- } else {
6
- module.exports = require("./spark-web-field.cjs.dev.js");
7
- }
10
+ // this bit of code imports the require hook and registers it
11
+ let unregister = require("../../../node_modules/@preconstruct/hook").___internalHook(typeof __dirname === 'undefined' ? undefined : __dirname, "../../..", "..");
12
+
13
+ // this re-exports the source file
14
+ module.exports = require("../src/index.ts");
15
+
16
+ unregister();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spark-web/field",
3
- "version": "3.0.6",
3
+ "version": "4.0.0-rc.0",
4
4
  "homepage": "https://github.com/brighte-labs/spark-web#readme",
5
5
  "repository": {
6
6
  "type": "git",
@@ -15,19 +15,19 @@
15
15
  "README.md"
16
16
  ],
17
17
  "dependencies": {
18
- "@babel/runtime": "^7.19.0",
19
- "@emotion/css": "^11.9.0",
20
- "@spark-web/a11y": "^1.3.2",
21
- "@spark-web/box": "^1.1.0",
22
- "@spark-web/icon": "^1.2.2",
23
- "@spark-web/stack": "^1.0.10",
24
- "@spark-web/text": "^1.1.2",
25
- "@spark-web/theme": "^3.1.0",
26
- "@spark-web/utils": "^1.2.3"
18
+ "@babel/runtime": "^7.25.0",
19
+ "@emotion/react": "^11.13.5",
20
+ "@spark-web/a11y": "^2.0.0-rc.0",
21
+ "@spark-web/box": "^2.0.0-rc.0",
22
+ "@spark-web/icon": "^2.0.0-rc.0",
23
+ "@spark-web/stack": "^2.0.0-rc.0",
24
+ "@spark-web/text": "^2.0.0-rc.0",
25
+ "@spark-web/theme": "^4.0.0-rc.0",
26
+ "@spark-web/utils": "^2.0.0-rc.0"
27
27
  },
28
28
  "devDependencies": {
29
- "@types/react": "^17.0.12",
30
- "react": "^17.0.2"
29
+ "@types/react": "^18.2.0",
30
+ "react": "^18.2.0"
31
31
  },
32
32
  "peerDependencies": {
33
33
  "react": ">=17.0.2"
@@ -1,15 +0,0 @@
1
- /// <reference types="react" />
2
- export declare type FieldState = {
3
- disabled: boolean;
4
- invalid: boolean;
5
- };
6
- export declare type InputPropsDerivedFromField = {
7
- 'aria-describedby'?: string;
8
- 'aria-invalid': true | undefined;
9
- id: string;
10
- };
11
- export declare type FieldContextType = [FieldState, InputPropsDerivedFromField];
12
- export declare const FieldContext: import("react").Context<FieldContextType | null>;
13
- export declare const FieldContextProvider: import("react").Provider<FieldContextType | null>;
14
- export declare const FIELD_CONTEXT_ERROR_MESSAGE = "Input components must be inside a `Field`.";
15
- export declare function useFieldContext(): FieldContextType;
@@ -1,51 +0,0 @@
1
- import type { DataAttributeMap } from '@spark-web/utils/internal';
2
- import type { ReactElement, ReactNode } from 'react';
3
- export declare type Tone = keyof typeof messageToneMap;
4
- export declare type FieldProps = {
5
- /** Sets a unique identifier for the component. */
6
- id?: string;
7
- /** Sets data attributes on the component. */
8
- data?: DataAttributeMap;
9
- /** Optionally provide a utility or contextual hint, related to the field. */
10
- adornment?: ReactElement;
11
- /** Input component */
12
- children: ReactNode;
13
- /**
14
- * Indicates that the field is perceivable but disabled, so it is not editable
15
- * or otherwise operable.
16
- */
17
- disabled?: boolean;
18
- /** Provide additional information that will aid user input. */
19
- description?: string;
20
- /** Concisely label the field. */
21
- label: string;
22
- /**
23
- * The label must always be provided for assistive technology, but you may
24
- * hide it from sighted users when the intent can be inferred from context.
25
- */
26
- labelVisibility?: 'hidden' | 'reserve-space' | 'visible';
27
- /** Provide a message, informing the user about changes in state. */
28
- message?: string;
29
- /** Additional context, typically used to indicate that the field is optional. */
30
- secondaryLabel?: string;
31
- /** Provide a tone to influence elements of the field, and its input. */
32
- tone?: Tone;
33
- };
34
- /**
35
- * Using a [context](https://reactjs.org/docs/context.html), the field
36
- * component connects the label, description, and message to the input element.
37
- */
38
- export declare const Field: import("react").ForwardRefExoticComponent<FieldProps & import("react").RefAttributes<HTMLDivElement>>;
39
- export declare function useFieldIds(id?: string): {
40
- descriptionId: string;
41
- inputId: string;
42
- messageId: string;
43
- };
44
- declare const messageToneMap: {
45
- readonly critical: "critical";
46
- readonly neutral: "muted";
47
- readonly positive: "positive";
48
- };
49
- declare type FieldMessageProps = Required<Pick<FieldProps, 'message' | 'id' | 'tone'>>;
50
- export declare const FieldMessage: ({ message, id, tone }: FieldMessageProps) => JSX.Element;
51
- export {};
@@ -1,4 +0,0 @@
1
- export { FieldContextProvider, useFieldContext } from './context';
2
- export { Field, FieldMessage, useFieldIds } from './field';
3
- export type { FieldContextType, FieldState, InputPropsDerivedFromField, } from './context';
4
- export type { FieldProps, Tone } from './field';
@@ -1,206 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var react = require('react');
6
- var css = require('@emotion/css');
7
- var a11y = require('@spark-web/a11y');
8
- var box = require('@spark-web/box');
9
- var icon = require('@spark-web/icon');
10
- var stack = require('@spark-web/stack');
11
- var text = require('@spark-web/text');
12
- var theme = require('@spark-web/theme');
13
- var jsxRuntime = require('react/jsx-runtime');
14
-
15
- var FieldContext = /*#__PURE__*/react.createContext(null);
16
- var FieldContextProvider = FieldContext.Provider;
17
- var FIELD_CONTEXT_ERROR_MESSAGE = 'Input components must be inside a `Field`.';
18
- function useFieldContext() {
19
- var ctx = react.useContext(FieldContext);
20
-
21
- if (!ctx) {
22
- throw new Error(FIELD_CONTEXT_ERROR_MESSAGE);
23
- }
24
-
25
- return ctx;
26
- }
27
-
28
- /**
29
- * Using a [context](https://reactjs.org/docs/context.html), the field
30
- * component connects the label, description, and message to the input element.
31
- */
32
- var Field = /*#__PURE__*/react.forwardRef(function (_ref, forwardedRef) {
33
- var children = _ref.children,
34
- idProp = _ref.id,
35
- data = _ref.data,
36
- description = _ref.description,
37
- _ref$disabled = _ref.disabled,
38
- disabled = _ref$disabled === void 0 ? false : _ref$disabled,
39
- label = _ref.label,
40
- adornment = _ref.adornment,
41
- _ref$labelVisibility = _ref.labelVisibility,
42
- labelVisibility = _ref$labelVisibility === void 0 ? 'visible' : _ref$labelVisibility,
43
- message = _ref.message,
44
- secondaryLabel = _ref.secondaryLabel,
45
- _ref$tone = _ref.tone,
46
- tone = _ref$tone === void 0 ? 'neutral' : _ref$tone;
47
-
48
- var _useFieldIds = useFieldIds(idProp),
49
- descriptionId = _useFieldIds.descriptionId,
50
- inputId = _useFieldIds.inputId,
51
- messageId = _useFieldIds.messageId; // field context
52
-
53
-
54
- var invalid = Boolean(message && tone === 'critical');
55
- var fieldContext = react.useMemo(function () {
56
- return [{
57
- disabled: disabled,
58
- invalid: invalid
59
- }, {
60
- 'aria-describedby': a11y.mergeIds(message && messageId, description && descriptionId),
61
- 'aria-invalid': invalid || undefined,
62
- id: inputId
63
- }];
64
- }, [description, descriptionId, disabled, inputId, invalid, message, messageId]); // label prep
65
-
66
- var hiddenLabel = /*#__PURE__*/jsxRuntime.jsxs(a11y.VisuallyHidden, {
67
- as: "label",
68
- htmlFor: inputId,
69
- children: [label, " ", secondaryLabel]
70
- });
71
-
72
- var labelElement = {
73
- hidden: hiddenLabel,
74
- visible: /*#__PURE__*/jsxRuntime.jsx(box.Box, {
75
- as: "label",
76
- htmlFor: inputId,
77
- children: /*#__PURE__*/jsxRuntime.jsxs(text.Text, {
78
- tone: disabled ? 'disabled' : 'neutral',
79
- weight: "semibold",
80
- children: [label, ' ', secondaryLabel && /*#__PURE__*/jsxRuntime.jsx(text.Text, {
81
- inline: true,
82
- tone: disabled ? 'disabled' : 'muted',
83
- weight: "regular",
84
- children: secondaryLabel
85
- })]
86
- })
87
- }),
88
- 'reserve-space': /*#__PURE__*/jsxRuntime.jsxs(react.Fragment, {
89
- children: [hiddenLabel, /*#__PURE__*/jsxRuntime.jsx(text.Text, {
90
- "aria-hidden": true,
91
- children: "\xA0"
92
- })]
93
- })
94
- };
95
- var LabelWrapper = labelVisibility === 'hidden' ? react.Fragment : FieldLabelWrapper;
96
- return /*#__PURE__*/jsxRuntime.jsx(FieldContextProvider, {
97
- value: fieldContext,
98
- children: /*#__PURE__*/jsxRuntime.jsxs(stack.Stack, {
99
- ref: forwardedRef,
100
- data: data,
101
- gap: "medium",
102
- children: [/*#__PURE__*/jsxRuntime.jsxs(LabelWrapper, {
103
- children: [labelElement[labelVisibility], adornment]
104
- }), description && /*#__PURE__*/jsxRuntime.jsx(text.Text, {
105
- tone: "muted",
106
- size: "small",
107
- id: descriptionId,
108
- children: description
109
- }), children, message && /*#__PURE__*/jsxRuntime.jsx(FieldMessage, {
110
- tone: tone,
111
- id: messageId,
112
- message: message
113
- })]
114
- })
115
- });
116
- });
117
- Field.displayName = 'Field'; // Utils
118
- // ------------------------------
119
-
120
- function useFieldIds(id) {
121
- var inputId = a11y.useId(id);
122
- var descriptionId = a11y.composeId(inputId, 'description');
123
- var messageId = a11y.composeId(inputId, 'message');
124
- return {
125
- descriptionId: descriptionId,
126
- inputId: inputId,
127
- messageId: messageId
128
- };
129
- } // Styled components
130
- // ------------------------------
131
-
132
- function FieldLabelWrapper(_ref2) {
133
- var children = _ref2.children;
134
- return /*#__PURE__*/jsxRuntime.jsx(box.Box, {
135
- display: "flex",
136
- alignItems: "center",
137
- justifyContent: "spaceBetween",
138
- gap: "large",
139
- children: children
140
- });
141
- }
142
-
143
- var messageToneMap = {
144
- critical: 'critical',
145
- neutral: 'muted',
146
- positive: 'positive'
147
- }; // NOTE: use icons in addition to color for folks with visions issues
148
-
149
- var messageIconMap = {
150
- critical: icon.ExclamationCircleIcon,
151
- neutral: null,
152
- positive: icon.CheckCircleIcon
153
- };
154
- var FieldMessage = function FieldMessage(_ref3) {
155
- var message = _ref3.message,
156
- id = _ref3.id,
157
- tone = _ref3.tone;
158
- var textTone = messageToneMap[tone];
159
- var Icon = messageIconMap[tone];
160
- return /*#__PURE__*/jsxRuntime.jsxs(box.Box, {
161
- display: "flex",
162
- gap: "xsmall",
163
- children: [Icon ? /*#__PURE__*/jsxRuntime.jsx(IndicatorContainer, {
164
- children: /*#__PURE__*/jsxRuntime.jsx(Icon, {
165
- size: "xxsmall",
166
- tone: tone
167
- })
168
- }) : null, /*#__PURE__*/jsxRuntime.jsx(text.Text, {
169
- tone: textTone,
170
- size: "small",
171
- id: id,
172
- children: message
173
- })]
174
- });
175
- };
176
-
177
- function IndicatorContainer(_ref4) {
178
- var children = _ref4.children;
179
- var theme$1 = theme.useTheme();
180
- var _theme$typography$tex = theme$1.typography.text.small,
181
- mobile = _theme$typography$tex.mobile,
182
- tablet = _theme$typography$tex.tablet;
183
- var responsiveStyles = theme$1.utils.responsiveStyles({
184
- mobile: {
185
- height: mobile.capHeight
186
- },
187
- tablet: {
188
- height: tablet.capHeight
189
- }
190
- });
191
- return /*#__PURE__*/jsxRuntime.jsx(box.Box, {
192
- display: "flex",
193
- alignItems: "center",
194
- "aria-hidden": true,
195
- cursor: "default",
196
- flexShrink: 0,
197
- className: css.css(responsiveStyles),
198
- children: children
199
- });
200
- }
201
-
202
- exports.Field = Field;
203
- exports.FieldContextProvider = FieldContextProvider;
204
- exports.FieldMessage = FieldMessage;
205
- exports.useFieldContext = useFieldContext;
206
- exports.useFieldIds = useFieldIds;
@@ -1,206 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var react = require('react');
6
- var css = require('@emotion/css');
7
- var a11y = require('@spark-web/a11y');
8
- var box = require('@spark-web/box');
9
- var icon = require('@spark-web/icon');
10
- var stack = require('@spark-web/stack');
11
- var text = require('@spark-web/text');
12
- var theme = require('@spark-web/theme');
13
- var jsxRuntime = require('react/jsx-runtime');
14
-
15
- var FieldContext = /*#__PURE__*/react.createContext(null);
16
- var FieldContextProvider = FieldContext.Provider;
17
- var FIELD_CONTEXT_ERROR_MESSAGE = 'Input components must be inside a `Field`.';
18
- function useFieldContext() {
19
- var ctx = react.useContext(FieldContext);
20
-
21
- if (!ctx) {
22
- throw new Error(FIELD_CONTEXT_ERROR_MESSAGE);
23
- }
24
-
25
- return ctx;
26
- }
27
-
28
- /**
29
- * Using a [context](https://reactjs.org/docs/context.html), the field
30
- * component connects the label, description, and message to the input element.
31
- */
32
- var Field = /*#__PURE__*/react.forwardRef(function (_ref, forwardedRef) {
33
- var children = _ref.children,
34
- idProp = _ref.id,
35
- data = _ref.data,
36
- description = _ref.description,
37
- _ref$disabled = _ref.disabled,
38
- disabled = _ref$disabled === void 0 ? false : _ref$disabled,
39
- label = _ref.label,
40
- adornment = _ref.adornment,
41
- _ref$labelVisibility = _ref.labelVisibility,
42
- labelVisibility = _ref$labelVisibility === void 0 ? 'visible' : _ref$labelVisibility,
43
- message = _ref.message,
44
- secondaryLabel = _ref.secondaryLabel,
45
- _ref$tone = _ref.tone,
46
- tone = _ref$tone === void 0 ? 'neutral' : _ref$tone;
47
-
48
- var _useFieldIds = useFieldIds(idProp),
49
- descriptionId = _useFieldIds.descriptionId,
50
- inputId = _useFieldIds.inputId,
51
- messageId = _useFieldIds.messageId; // field context
52
-
53
-
54
- var invalid = Boolean(message && tone === 'critical');
55
- var fieldContext = react.useMemo(function () {
56
- return [{
57
- disabled: disabled,
58
- invalid: invalid
59
- }, {
60
- 'aria-describedby': a11y.mergeIds(message && messageId, description && descriptionId),
61
- 'aria-invalid': invalid || undefined,
62
- id: inputId
63
- }];
64
- }, [description, descriptionId, disabled, inputId, invalid, message, messageId]); // label prep
65
-
66
- var hiddenLabel = /*#__PURE__*/jsxRuntime.jsxs(a11y.VisuallyHidden, {
67
- as: "label",
68
- htmlFor: inputId,
69
- children: [label, " ", secondaryLabel]
70
- });
71
-
72
- var labelElement = {
73
- hidden: hiddenLabel,
74
- visible: /*#__PURE__*/jsxRuntime.jsx(box.Box, {
75
- as: "label",
76
- htmlFor: inputId,
77
- children: /*#__PURE__*/jsxRuntime.jsxs(text.Text, {
78
- tone: disabled ? 'disabled' : 'neutral',
79
- weight: "semibold",
80
- children: [label, ' ', secondaryLabel && /*#__PURE__*/jsxRuntime.jsx(text.Text, {
81
- inline: true,
82
- tone: disabled ? 'disabled' : 'muted',
83
- weight: "regular",
84
- children: secondaryLabel
85
- })]
86
- })
87
- }),
88
- 'reserve-space': /*#__PURE__*/jsxRuntime.jsxs(react.Fragment, {
89
- children: [hiddenLabel, /*#__PURE__*/jsxRuntime.jsx(text.Text, {
90
- "aria-hidden": true,
91
- children: "\xA0"
92
- })]
93
- })
94
- };
95
- var LabelWrapper = labelVisibility === 'hidden' ? react.Fragment : FieldLabelWrapper;
96
- return /*#__PURE__*/jsxRuntime.jsx(FieldContextProvider, {
97
- value: fieldContext,
98
- children: /*#__PURE__*/jsxRuntime.jsxs(stack.Stack, {
99
- ref: forwardedRef,
100
- data: data,
101
- gap: "medium",
102
- children: [/*#__PURE__*/jsxRuntime.jsxs(LabelWrapper, {
103
- children: [labelElement[labelVisibility], adornment]
104
- }), description && /*#__PURE__*/jsxRuntime.jsx(text.Text, {
105
- tone: "muted",
106
- size: "small",
107
- id: descriptionId,
108
- children: description
109
- }), children, message && /*#__PURE__*/jsxRuntime.jsx(FieldMessage, {
110
- tone: tone,
111
- id: messageId,
112
- message: message
113
- })]
114
- })
115
- });
116
- });
117
- Field.displayName = 'Field'; // Utils
118
- // ------------------------------
119
-
120
- function useFieldIds(id) {
121
- var inputId = a11y.useId(id);
122
- var descriptionId = a11y.composeId(inputId, 'description');
123
- var messageId = a11y.composeId(inputId, 'message');
124
- return {
125
- descriptionId: descriptionId,
126
- inputId: inputId,
127
- messageId: messageId
128
- };
129
- } // Styled components
130
- // ------------------------------
131
-
132
- function FieldLabelWrapper(_ref2) {
133
- var children = _ref2.children;
134
- return /*#__PURE__*/jsxRuntime.jsx(box.Box, {
135
- display: "flex",
136
- alignItems: "center",
137
- justifyContent: "spaceBetween",
138
- gap: "large",
139
- children: children
140
- });
141
- }
142
-
143
- var messageToneMap = {
144
- critical: 'critical',
145
- neutral: 'muted',
146
- positive: 'positive'
147
- }; // NOTE: use icons in addition to color for folks with visions issues
148
-
149
- var messageIconMap = {
150
- critical: icon.ExclamationCircleIcon,
151
- neutral: null,
152
- positive: icon.CheckCircleIcon
153
- };
154
- var FieldMessage = function FieldMessage(_ref3) {
155
- var message = _ref3.message,
156
- id = _ref3.id,
157
- tone = _ref3.tone;
158
- var textTone = messageToneMap[tone];
159
- var Icon = messageIconMap[tone];
160
- return /*#__PURE__*/jsxRuntime.jsxs(box.Box, {
161
- display: "flex",
162
- gap: "xsmall",
163
- children: [Icon ? /*#__PURE__*/jsxRuntime.jsx(IndicatorContainer, {
164
- children: /*#__PURE__*/jsxRuntime.jsx(Icon, {
165
- size: "xxsmall",
166
- tone: tone
167
- })
168
- }) : null, /*#__PURE__*/jsxRuntime.jsx(text.Text, {
169
- tone: textTone,
170
- size: "small",
171
- id: id,
172
- children: message
173
- })]
174
- });
175
- };
176
-
177
- function IndicatorContainer(_ref4) {
178
- var children = _ref4.children;
179
- var theme$1 = theme.useTheme();
180
- var _theme$typography$tex = theme$1.typography.text.small,
181
- mobile = _theme$typography$tex.mobile,
182
- tablet = _theme$typography$tex.tablet;
183
- var responsiveStyles = theme$1.utils.responsiveStyles({
184
- mobile: {
185
- height: mobile.capHeight
186
- },
187
- tablet: {
188
- height: tablet.capHeight
189
- }
190
- });
191
- return /*#__PURE__*/jsxRuntime.jsx(box.Box, {
192
- display: "flex",
193
- alignItems: "center",
194
- "aria-hidden": true,
195
- cursor: "default",
196
- flexShrink: 0,
197
- className: css.css(responsiveStyles),
198
- children: children
199
- });
200
- }
201
-
202
- exports.Field = Field;
203
- exports.FieldContextProvider = FieldContextProvider;
204
- exports.FieldMessage = FieldMessage;
205
- exports.useFieldContext = useFieldContext;
206
- exports.useFieldIds = useFieldIds;
@@ -1,198 +0,0 @@
1
- import { createContext, useContext, forwardRef, useMemo, Fragment } from 'react';
2
- import { css } from '@emotion/css';
3
- import { mergeIds, useId, composeId, VisuallyHidden } from '@spark-web/a11y';
4
- import { Box } from '@spark-web/box';
5
- import { ExclamationCircleIcon, CheckCircleIcon } from '@spark-web/icon';
6
- import { Stack } from '@spark-web/stack';
7
- import { Text } from '@spark-web/text';
8
- import { useTheme } from '@spark-web/theme';
9
- import { jsx, jsxs } from 'react/jsx-runtime';
10
-
11
- var FieldContext = /*#__PURE__*/createContext(null);
12
- var FieldContextProvider = FieldContext.Provider;
13
- var FIELD_CONTEXT_ERROR_MESSAGE = 'Input components must be inside a `Field`.';
14
- function useFieldContext() {
15
- var ctx = useContext(FieldContext);
16
-
17
- if (!ctx) {
18
- throw new Error(FIELD_CONTEXT_ERROR_MESSAGE);
19
- }
20
-
21
- return ctx;
22
- }
23
-
24
- /**
25
- * Using a [context](https://reactjs.org/docs/context.html), the field
26
- * component connects the label, description, and message to the input element.
27
- */
28
- var Field = /*#__PURE__*/forwardRef(function (_ref, forwardedRef) {
29
- var children = _ref.children,
30
- idProp = _ref.id,
31
- data = _ref.data,
32
- description = _ref.description,
33
- _ref$disabled = _ref.disabled,
34
- disabled = _ref$disabled === void 0 ? false : _ref$disabled,
35
- label = _ref.label,
36
- adornment = _ref.adornment,
37
- _ref$labelVisibility = _ref.labelVisibility,
38
- labelVisibility = _ref$labelVisibility === void 0 ? 'visible' : _ref$labelVisibility,
39
- message = _ref.message,
40
- secondaryLabel = _ref.secondaryLabel,
41
- _ref$tone = _ref.tone,
42
- tone = _ref$tone === void 0 ? 'neutral' : _ref$tone;
43
-
44
- var _useFieldIds = useFieldIds(idProp),
45
- descriptionId = _useFieldIds.descriptionId,
46
- inputId = _useFieldIds.inputId,
47
- messageId = _useFieldIds.messageId; // field context
48
-
49
-
50
- var invalid = Boolean(message && tone === 'critical');
51
- var fieldContext = useMemo(function () {
52
- return [{
53
- disabled: disabled,
54
- invalid: invalid
55
- }, {
56
- 'aria-describedby': mergeIds(message && messageId, description && descriptionId),
57
- 'aria-invalid': invalid || undefined,
58
- id: inputId
59
- }];
60
- }, [description, descriptionId, disabled, inputId, invalid, message, messageId]); // label prep
61
-
62
- var hiddenLabel = /*#__PURE__*/jsxs(VisuallyHidden, {
63
- as: "label",
64
- htmlFor: inputId,
65
- children: [label, " ", secondaryLabel]
66
- });
67
-
68
- var labelElement = {
69
- hidden: hiddenLabel,
70
- visible: /*#__PURE__*/jsx(Box, {
71
- as: "label",
72
- htmlFor: inputId,
73
- children: /*#__PURE__*/jsxs(Text, {
74
- tone: disabled ? 'disabled' : 'neutral',
75
- weight: "semibold",
76
- children: [label, ' ', secondaryLabel && /*#__PURE__*/jsx(Text, {
77
- inline: true,
78
- tone: disabled ? 'disabled' : 'muted',
79
- weight: "regular",
80
- children: secondaryLabel
81
- })]
82
- })
83
- }),
84
- 'reserve-space': /*#__PURE__*/jsxs(Fragment, {
85
- children: [hiddenLabel, /*#__PURE__*/jsx(Text, {
86
- "aria-hidden": true,
87
- children: "\xA0"
88
- })]
89
- })
90
- };
91
- var LabelWrapper = labelVisibility === 'hidden' ? Fragment : FieldLabelWrapper;
92
- return /*#__PURE__*/jsx(FieldContextProvider, {
93
- value: fieldContext,
94
- children: /*#__PURE__*/jsxs(Stack, {
95
- ref: forwardedRef,
96
- data: data,
97
- gap: "medium",
98
- children: [/*#__PURE__*/jsxs(LabelWrapper, {
99
- children: [labelElement[labelVisibility], adornment]
100
- }), description && /*#__PURE__*/jsx(Text, {
101
- tone: "muted",
102
- size: "small",
103
- id: descriptionId,
104
- children: description
105
- }), children, message && /*#__PURE__*/jsx(FieldMessage, {
106
- tone: tone,
107
- id: messageId,
108
- message: message
109
- })]
110
- })
111
- });
112
- });
113
- Field.displayName = 'Field'; // Utils
114
- // ------------------------------
115
-
116
- function useFieldIds(id) {
117
- var inputId = useId(id);
118
- var descriptionId = composeId(inputId, 'description');
119
- var messageId = composeId(inputId, 'message');
120
- return {
121
- descriptionId: descriptionId,
122
- inputId: inputId,
123
- messageId: messageId
124
- };
125
- } // Styled components
126
- // ------------------------------
127
-
128
- function FieldLabelWrapper(_ref2) {
129
- var children = _ref2.children;
130
- return /*#__PURE__*/jsx(Box, {
131
- display: "flex",
132
- alignItems: "center",
133
- justifyContent: "spaceBetween",
134
- gap: "large",
135
- children: children
136
- });
137
- }
138
-
139
- var messageToneMap = {
140
- critical: 'critical',
141
- neutral: 'muted',
142
- positive: 'positive'
143
- }; // NOTE: use icons in addition to color for folks with visions issues
144
-
145
- var messageIconMap = {
146
- critical: ExclamationCircleIcon,
147
- neutral: null,
148
- positive: CheckCircleIcon
149
- };
150
- var FieldMessage = function FieldMessage(_ref3) {
151
- var message = _ref3.message,
152
- id = _ref3.id,
153
- tone = _ref3.tone;
154
- var textTone = messageToneMap[tone];
155
- var Icon = messageIconMap[tone];
156
- return /*#__PURE__*/jsxs(Box, {
157
- display: "flex",
158
- gap: "xsmall",
159
- children: [Icon ? /*#__PURE__*/jsx(IndicatorContainer, {
160
- children: /*#__PURE__*/jsx(Icon, {
161
- size: "xxsmall",
162
- tone: tone
163
- })
164
- }) : null, /*#__PURE__*/jsx(Text, {
165
- tone: textTone,
166
- size: "small",
167
- id: id,
168
- children: message
169
- })]
170
- });
171
- };
172
-
173
- function IndicatorContainer(_ref4) {
174
- var children = _ref4.children;
175
- var theme = useTheme();
176
- var _theme$typography$tex = theme.typography.text.small,
177
- mobile = _theme$typography$tex.mobile,
178
- tablet = _theme$typography$tex.tablet;
179
- var responsiveStyles = theme.utils.responsiveStyles({
180
- mobile: {
181
- height: mobile.capHeight
182
- },
183
- tablet: {
184
- height: tablet.capHeight
185
- }
186
- });
187
- return /*#__PURE__*/jsx(Box, {
188
- display: "flex",
189
- alignItems: "center",
190
- "aria-hidden": true,
191
- cursor: "default",
192
- flexShrink: 0,
193
- className: css(responsiveStyles),
194
- children: children
195
- });
196
- }
197
-
198
- export { Field, FieldContextProvider, FieldMessage, useFieldContext, useFieldIds };