@seed-design/react-field 0.0.0-alpha-20260414104312

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,192 @@
1
+ import { ariaAttr, dataAttr, elementProps, inputProps, labelProps } from "@seed-design/dom-utils";
2
+ import { useCallback, useId, useState } from "react";
3
+ import { getDescriptionId, getErrorMessageId, getInputId, getLabelId } from "./dom";
4
+ import { useSupports } from "@seed-design/react-supports";
5
+
6
+ function useFieldState() {
7
+ const [isHovered, setIsHovered] = useState(false);
8
+ const [isActive, setIsActive] = useState(false);
9
+ const [isFocused, setIsFocused] = useState(false);
10
+ const [isFocusVisible, setIsFocusVisible] = useState(false);
11
+
12
+ const [isLabelRendered, setIsLabelRendered] = useState(false);
13
+ const labelRef = useCallback((node: HTMLLabelElement | null) => {
14
+ setIsLabelRendered(!!node);
15
+ }, []);
16
+ const [isDescriptionRendered, setIsDescriptionRendered] = useState(false);
17
+ const descriptionRef = useCallback((node: HTMLElement | null) => {
18
+ setIsDescriptionRendered(!!node);
19
+ }, []);
20
+ const [isErrorMessageRendered, setIsErrorMessageRendered] = useState(false);
21
+ const errorMessageRef = useCallback((node: HTMLElement | null) => {
22
+ setIsErrorMessageRendered(!!node);
23
+ }, []);
24
+
25
+ return {
26
+ refs: {
27
+ label: labelRef,
28
+ description: descriptionRef,
29
+ errorMessage: errorMessageRef,
30
+ },
31
+
32
+ isHovered,
33
+ isActive,
34
+ isFocused,
35
+ isFocusVisible,
36
+ renderedElements: {
37
+ label: isLabelRendered,
38
+ description: isDescriptionRendered,
39
+ errorMessage: isErrorMessageRendered,
40
+ },
41
+
42
+ setIsHovered,
43
+ setIsActive,
44
+ setIsFocused,
45
+ setIsFocusVisible,
46
+ };
47
+ }
48
+
49
+ export interface UseFieldProps {
50
+ /**
51
+ * @default false
52
+ */
53
+ required?: boolean;
54
+ /**
55
+ * @default false
56
+ */
57
+ disabled?: boolean;
58
+ /**
59
+ * @default false
60
+ */
61
+ readOnly?: boolean;
62
+ /**
63
+ * @default false
64
+ */
65
+ invalid?: boolean;
66
+
67
+ name?: string;
68
+ }
69
+
70
+ export type UseFieldReturn = ReturnType<typeof useField>;
71
+
72
+ export function useField(props: UseFieldProps) {
73
+ const id = useId();
74
+ const { disabled = false, invalid = false, readOnly = false, required = false } = props;
75
+
76
+ const isFocusVisibleSupported = useSupports("selector(:focus-visible)");
77
+
78
+ const {
79
+ refs,
80
+ renderedElements,
81
+ isHovered,
82
+ isActive,
83
+ isFocused,
84
+ isFocusVisible,
85
+ setIsHovered,
86
+ setIsActive,
87
+ setIsFocused,
88
+ setIsFocusVisible,
89
+ } = useFieldState();
90
+
91
+ const ariaDescribedBy =
92
+ [
93
+ renderedElements.description ? getDescriptionId(id) : false,
94
+ renderedElements.errorMessage ? getErrorMessageId(id) : false,
95
+ ]
96
+ .filter(Boolean)
97
+ .join(" ") || undefined;
98
+
99
+ const stateProps = elementProps({
100
+ "data-hover": dataAttr(isHovered),
101
+ "data-active": dataAttr(isActive),
102
+ "data-focus": dataAttr(isFocused),
103
+ "data-focus-visible": dataAttr(isFocusVisible),
104
+ "data-readonly": dataAttr(readOnly),
105
+ "data-disabled": dataAttr(disabled),
106
+ "data-invalid": dataAttr(invalid),
107
+ });
108
+
109
+ return {
110
+ refs,
111
+
112
+ active: isActive,
113
+ focused: isFocused,
114
+ invalid,
115
+ required,
116
+
117
+ setIsFocused,
118
+ setIsFocusVisible,
119
+
120
+ stateProps,
121
+
122
+ rootProps: elementProps({
123
+ ...stateProps,
124
+
125
+ onPointerMove() {
126
+ setIsHovered(true);
127
+ },
128
+ onPointerDown() {
129
+ setIsActive(true);
130
+ },
131
+ onPointerUp() {
132
+ setIsActive(false);
133
+ },
134
+ onPointerLeave() {
135
+ setIsHovered(false);
136
+ setIsActive(false);
137
+ },
138
+ }),
139
+
140
+ labelProps: labelProps({
141
+ ...stateProps,
142
+ id: getLabelId(id),
143
+ htmlFor: getInputId(id),
144
+ }),
145
+
146
+ inputProps: inputProps({
147
+ disabled,
148
+ readOnly,
149
+ name: props.name || id,
150
+ id: getInputId(id),
151
+ }),
152
+
153
+ inputAriaAttributes: elementProps({
154
+ ...(renderedElements.label && { "aria-labelledby": getLabelId(id) }),
155
+ "aria-describedby": ariaDescribedBy,
156
+ "aria-required": ariaAttr(required),
157
+ "aria-invalid": ariaAttr(invalid),
158
+ "aria-readonly": ariaAttr(readOnly),
159
+ "aria-disabled": ariaAttr(disabled),
160
+ }),
161
+ inputHandlers: inputProps({
162
+ onChange: (event) => {
163
+ if (isFocusVisibleSupported) {
164
+ setIsFocusVisible(event.target.matches(":focus-visible"));
165
+ }
166
+ },
167
+ onBlur() {
168
+ setIsFocused(false);
169
+ if (isFocusVisibleSupported) {
170
+ setIsFocusVisible(false);
171
+ }
172
+ },
173
+ onFocus(event) {
174
+ setIsFocused(true);
175
+ if (isFocusVisibleSupported) {
176
+ setIsFocusVisible(event.target.matches(":focus-visible"));
177
+ }
178
+ },
179
+ }),
180
+
181
+ descriptionProps: elementProps({
182
+ ...stateProps,
183
+ id: getDescriptionId(id),
184
+ }),
185
+
186
+ errorMessageProps: elementProps({
187
+ ...stateProps,
188
+ id: getErrorMessageId(id),
189
+ "aria-live": "polite",
190
+ }),
191
+ };
192
+ }
@@ -0,0 +1,21 @@
1
+ import { createContext, useContext } from "react";
2
+ import type { UseFieldReturn } from "./useField";
3
+
4
+ export interface UseFieldContext extends UseFieldReturn {}
5
+
6
+ const FieldContext = createContext<UseFieldContext | null>(null);
7
+
8
+ export const FieldProvider = FieldContext.Provider;
9
+
10
+ export function useFieldContext<T extends boolean | undefined = true>({
11
+ strict = true,
12
+ }: {
13
+ strict?: T;
14
+ } = {}): T extends false ? UseFieldContext | null : UseFieldContext {
15
+ const context = useContext(FieldContext);
16
+ if (!context && strict) {
17
+ throw new Error("useFieldContext must be used within a Field");
18
+ }
19
+
20
+ return context as UseFieldContext;
21
+ }