ui-ingredients 0.0.31 → 0.0.32

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -46,6 +46,7 @@ npm install ui-ingredients
46
46
  - 🟢 DatePicker
47
47
  - 🟢 Dialog
48
48
  - 🟢 Editable
49
+ - 🟢 Field
49
50
  - 🟢 FileUpload
50
51
  - ⚪ FloatingPanel
51
52
  - 🟢 HoverCard
@@ -1,12 +1,22 @@
1
1
  import { getEnvironmentContext } from '../environment-provider/context.svelte.js';
2
+ import { getFieldContext } from '../field/context.svelte.js';
2
3
  import { getLocaleContext } from '../locale-provider/context.svelte.js';
3
4
  import * as checkbox from '@zag-js/checkbox';
4
5
  import { normalizeProps, reflect, useMachine } from '@zag-js/svelte';
5
6
  import { uid } from 'uid';
6
7
  export function createCheckbox(props) {
8
+ const field = getFieldContext();
7
9
  const locale = getLocaleContext();
8
10
  const environment = getEnvironmentContext();
9
11
  const [state, send] = useMachine(checkbox.machine({
12
+ ids: {
13
+ label: field?.ids.label,
14
+ hiddenInput: field?.ids.control,
15
+ },
16
+ invalid: field?.invalid,
17
+ disabled: field?.disabled,
18
+ readOnly: field?.readOnly,
19
+ required: field?.required,
10
20
  ...props,
11
21
  id: props.id ?? uid(),
12
22
  dir: locale?.dir,
@@ -1,4 +1,5 @@
1
1
  import { getEnvironmentContext } from '../environment-provider/context.svelte.js';
2
+ import { getFieldContext } from '../field/context.svelte.js';
2
3
  import { getLocaleContext } from '../locale-provider/context.svelte.js';
3
4
  import * as combobox from '@zag-js/combobox';
4
5
  import { normalizeProps, reflect, useMachine } from '@zag-js/svelte';
@@ -10,9 +11,18 @@ export function createCombobox(props) {
10
11
  itemToString: (item) => props.itemToString ? props.itemToString(item) : String(item),
11
12
  isItemDisabled: (item) => props.isItemDisabled ? props.isItemDisabled(item) : false,
12
13
  }));
14
+ const field = getFieldContext();
13
15
  const locale = getLocaleContext();
14
16
  const environment = getEnvironmentContext();
15
17
  const [state, send] = useMachine(combobox.machine({
18
+ ids: {
19
+ label: field?.ids.label,
20
+ input: field?.ids.control,
21
+ },
22
+ invalid: field?.invalid,
23
+ required: field?.required,
24
+ disabled: field?.disabled,
25
+ readOnly: field?.readOnly,
16
26
  ...props,
17
27
  id: props.id ?? uid(),
18
28
  dir: locale?.dir,
@@ -1,2 +1,2 @@
1
- export declare const anatomy: import("@zag-js/anatomy").AnatomyInstance<"input" | "label" | "table" | "view" | "root" | "content" | "clearTrigger" | "control" | "monthSelect" | "nextTrigger" | "positioner" | "prevTrigger" | "rangeText" | "tableBody" | "tableCell" | "tableCellTrigger" | "tableHead" | "tableHeader" | "tableRow" | "trigger" | "viewTrigger" | "viewControl" | "yearSelect" | "presetTrigger">;
2
- export declare const parts: Record<"input" | "label" | "table" | "view" | "root" | "content" | "clearTrigger" | "control" | "monthSelect" | "nextTrigger" | "positioner" | "prevTrigger" | "rangeText" | "tableBody" | "tableCell" | "tableCellTrigger" | "tableHead" | "tableHeader" | "tableRow" | "trigger" | "viewTrigger" | "viewControl" | "yearSelect" | "presetTrigger", import("@zag-js/anatomy").AnatomyPart>;
1
+ export declare const anatomy: import("@zag-js/anatomy").AnatomyInstance<"input" | "label" | "table" | "view" | "root" | "control" | "content" | "trigger" | "clearTrigger" | "positioner" | "monthSelect" | "nextTrigger" | "prevTrigger" | "rangeText" | "tableBody" | "tableCell" | "tableCellTrigger" | "tableHead" | "tableHeader" | "tableRow" | "viewTrigger" | "viewControl" | "yearSelect" | "presetTrigger">;
2
+ export declare const parts: Record<"input" | "label" | "table" | "view" | "root" | "control" | "content" | "trigger" | "clearTrigger" | "positioner" | "monthSelect" | "nextTrigger" | "prevTrigger" | "rangeText" | "tableBody" | "tableCell" | "tableCellTrigger" | "tableHead" | "tableHeader" | "tableRow" | "viewTrigger" | "viewControl" | "yearSelect" | "presetTrigger", import("@zag-js/anatomy").AnatomyPart>;
@@ -1,12 +1,22 @@
1
1
  import { getEnvironmentContext } from '../environment-provider/context.svelte.js';
2
+ import { getFieldContext } from '../field/context.svelte.js';
2
3
  import { getLocaleContext } from '../locale-provider/context.svelte.js';
3
4
  import * as editable from '@zag-js/editable';
4
5
  import { normalizeProps, reflect, useMachine } from '@zag-js/svelte';
5
6
  import { uid } from 'uid';
6
7
  export function createEditable(props) {
8
+ const field = getFieldContext();
7
9
  const locale = getLocaleContext();
8
10
  const environment = getEnvironmentContext();
9
11
  const [state, send] = useMachine(editable.machine({
12
+ ids: {
13
+ label: field?.ids.label,
14
+ input: field?.ids.control,
15
+ },
16
+ invalid: field?.invalid,
17
+ disabled: field?.disabled,
18
+ readOnly: field?.readOnly,
19
+ required: field?.required,
10
20
  ...props,
11
21
  id: props.id ?? uid(),
12
22
  dir: locale?.dir,
@@ -1,3 +1,4 @@
1
1
  import { Context } from '../context.svelte.js';
2
2
  import type { CreateFieldReturn } from './create-field.svelte.js';
3
3
  export declare const fieldContext: Context<CreateFieldReturn>;
4
+ export declare const getFieldContext: () => CreateFieldReturn | null;
@@ -1,2 +1,3 @@
1
1
  import { Context } from '../context.svelte.js';
2
- export const fieldContext = new Context('Field');
2
+ export const fieldContext = new Context('Field', false);
3
+ export const getFieldContext = () => fieldContext.get();
@@ -17,6 +17,17 @@ export interface CreateFieldProps {
17
17
  export interface CreateFieldReturn extends ReturnType<typeof createField> {
18
18
  }
19
19
  export declare function createField(props: CreateFieldProps): {
20
+ readonly ids: {
21
+ root: string;
22
+ label: string;
23
+ control: string;
24
+ errorText: string;
25
+ helperText: string;
26
+ };
27
+ readonly disabled: boolean;
28
+ readonly required: boolean;
29
+ readonly readOnly: boolean;
30
+ readonly invalid: boolean;
20
31
  getRootProps: () => HTMLProps<"div">;
21
32
  getLabelProps: () => HTMLProps<"label">;
22
33
  getErrorTextProps: () => HTMLProps<"div">;
@@ -1,11 +1,13 @@
1
- import { ariaAttr, dataAttr } from '@zag-js/dom-query';
1
+ import { getEnvironmentContext } from '../environment-provider/context.svelte.js';
2
+ import { ariaAttr, dataAttr, getDocument, getWindow } from '@zag-js/dom-query';
2
3
  import { uid } from 'uid';
3
4
  import { parts } from './anatomy.js';
4
5
  export function createField(props) {
6
+ const environment = getEnvironmentContext();
5
7
  const id_ = uid();
6
8
  const {
7
9
  /**/
8
- ids, invalid, disabled, required, readonly, } = $derived.by(() => {
10
+ ids, invalid, disabled, required, readOnly, } = $derived.by(() => {
9
11
  const id = props.id ?? id_;
10
12
  const ids = {
11
13
  root: props.ids?.root ?? `field:${id}`,
@@ -19,7 +21,7 @@ export function createField(props) {
19
21
  invalid: props.invalid ?? false,
20
22
  required: props.required ?? false,
21
23
  disabled: props.disabled ?? false,
22
- readonly: props.readOnly ?? false,
24
+ readOnly: props.readOnly ?? false,
23
25
  };
24
26
  });
25
27
  let hasErrorText = $state(false);
@@ -35,14 +37,17 @@ export function createField(props) {
35
37
  return l.join(' ');
36
38
  });
37
39
  $effect(() => {
38
- if (invalid && document.getElementById(ids.errorText)) {
39
- hasErrorText = true;
40
- }
41
- });
42
- $effect(() => {
43
- if (document.getElementById(ids.helperText)) {
44
- hasHelperText = true;
40
+ const rootNode = environment?.getRootNode() ?? document;
41
+ const doc = getDocument(rootNode);
42
+ const win = getWindow(rootNode);
43
+ function handler() {
44
+ hasErrorText = invalid && doc.getElementById(ids.errorText) !== null;
45
+ hasHelperText = doc.getElementById(ids.helperText) !== null;
45
46
  }
47
+ handler();
48
+ const observer = new win.MutationObserver(handler);
49
+ observer.observe(rootNode, { childList: true, subtree: true });
50
+ return () => observer.disconnect();
46
51
  });
47
52
  function getRootProps() {
48
53
  return {
@@ -52,7 +57,7 @@ export function createField(props) {
52
57
  'data-invalid': dataAttr(invalid),
53
58
  'data-disabled': dataAttr(disabled),
54
59
  'data-required': dataAttr(required),
55
- 'data-readonly': dataAttr(readonly),
60
+ 'data-readonly': dataAttr(readOnly),
56
61
  };
57
62
  }
58
63
  function getLabelProps() {
@@ -63,7 +68,7 @@ export function createField(props) {
63
68
  'data-invalid': dataAttr(invalid),
64
69
  'data-disabled': dataAttr(disabled),
65
70
  'data-required': dataAttr(required),
66
- 'data-readonly': dataAttr(readonly),
71
+ 'data-readonly': dataAttr(readOnly),
67
72
  };
68
73
  }
69
74
  function getErrorTextProps() {
@@ -74,7 +79,7 @@ export function createField(props) {
74
79
  'data-invalid': dataAttr(invalid),
75
80
  'data-disabled': dataAttr(disabled),
76
81
  'data-required': dataAttr(required),
77
- 'data-readonly': dataAttr(readonly),
82
+ 'data-readonly': dataAttr(readOnly),
78
83
  };
79
84
  }
80
85
  function getHelperTextProps() {
@@ -83,7 +88,7 @@ export function createField(props) {
83
88
  'data-invalid': dataAttr(invalid),
84
89
  'data-disabled': dataAttr(disabled),
85
90
  'data-required': dataAttr(required),
86
- 'data-readonly': dataAttr(readonly),
91
+ 'data-readonly': dataAttr(readOnly),
87
92
  };
88
93
  }
89
94
  function getInputProps() {
@@ -92,13 +97,13 @@ export function createField(props) {
92
97
  id: ids.control,
93
98
  disabled,
94
99
  required,
95
- readonly,
100
+ readonly: readOnly,
96
101
  'aria-invalid': ariaAttr(invalid),
97
102
  'aria-describedby': ariaDescribedby,
98
103
  'data-invalid': dataAttr(invalid),
99
104
  'data-disabled': dataAttr(disabled),
100
105
  'data-required': dataAttr(required),
101
- 'data-readonly': dataAttr(readonly),
106
+ 'data-readonly': dataAttr(readOnly),
102
107
  };
103
108
  }
104
109
  function getSelectProps() {
@@ -108,12 +113,12 @@ export function createField(props) {
108
113
  disabled,
109
114
  required,
110
115
  'aria-invalid': ariaAttr(invalid),
111
- 'aria-readonly': ariaAttr(readonly),
116
+ 'aria-readonly': ariaAttr(readOnly),
112
117
  'aria-describedby': ariaDescribedby,
113
118
  'data-invalid': dataAttr(invalid),
114
119
  'data-disabled': dataAttr(disabled),
115
120
  'data-required': dataAttr(required),
116
- 'data-readonly': dataAttr(readonly),
121
+ 'data-readonly': dataAttr(readOnly),
117
122
  };
118
123
  }
119
124
  function getTextareaProps() {
@@ -122,15 +127,30 @@ export function createField(props) {
122
127
  id: ids.control,
123
128
  disabled,
124
129
  required,
125
- readonly,
130
+ readonly: readOnly,
126
131
  'aria-describedby': ariaDescribedby,
127
132
  'data-invalid': dataAttr(invalid),
128
133
  'data-disabled': dataAttr(disabled),
129
134
  'data-required': dataAttr(required),
130
- 'data-readonly': dataAttr(readonly),
135
+ 'data-readonly': dataAttr(readOnly),
131
136
  };
132
137
  }
133
138
  return {
139
+ get ids() {
140
+ return ids;
141
+ },
142
+ get disabled() {
143
+ return disabled;
144
+ },
145
+ get required() {
146
+ return required;
147
+ },
148
+ get readOnly() {
149
+ return readOnly;
150
+ },
151
+ get invalid() {
152
+ return invalid;
153
+ },
134
154
  getRootProps,
135
155
  getLabelProps,
136
156
  getErrorTextProps,
@@ -1,12 +1,20 @@
1
1
  import { getEnvironmentContext } from '../environment-provider/context.svelte.js';
2
+ import { getFieldContext } from '../field/context.svelte.js';
2
3
  import { getLocaleContext } from '../locale-provider/context.svelte.js';
3
4
  import * as fileUpload from '@zag-js/file-upload';
4
5
  import { normalizeProps, reflect, useMachine } from '@zag-js/svelte';
5
6
  import { uid } from 'uid';
6
7
  export function createFileUpload(props) {
8
+ const field = getFieldContext();
7
9
  const locale = getLocaleContext();
8
10
  const environment = getEnvironmentContext();
9
11
  const [state, send] = useMachine(fileUpload.machine({
12
+ ids: {
13
+ label: field?.ids.label,
14
+ hiddenInput: field?.ids.control,
15
+ },
16
+ disabled: field?.disabled,
17
+ required: field?.required,
10
18
  ...props,
11
19
  id: props.id ?? uid(),
12
20
  dir: locale?.dir,
@@ -1,12 +1,22 @@
1
1
  import { getEnvironmentContext } from '../environment-provider/context.svelte.js';
2
+ import { getFieldContext } from '../field/context.svelte.js';
2
3
  import { getLocaleContext } from '../locale-provider/context.svelte.js';
3
4
  import * as numberInput from '@zag-js/number-input';
4
5
  import { normalizeProps, reflect, useMachine } from '@zag-js/svelte';
5
6
  import { uid } from 'uid';
6
7
  export function createNumberInput(props) {
8
+ const field = getFieldContext();
7
9
  const locale = getLocaleContext();
8
10
  const environment = getEnvironmentContext();
9
11
  const [state, send] = useMachine(numberInput.machine({
12
+ ids: {
13
+ label: field?.ids.label,
14
+ input: field?.ids.control,
15
+ },
16
+ invalid: field?.invalid,
17
+ disabled: field?.disabled,
18
+ readOnly: field?.readOnly,
19
+ required: field?.required,
10
20
  ...props,
11
21
  id: props.id ?? uid(),
12
22
  dir: locale?.dir,
@@ -1,2 +1,2 @@
1
- export declare const anatomy: import("@zag-js/anatomy").AnatomyInstance<"input" | "label" | "root" | "clearTrigger" | "control">;
2
- export declare const parts: Record<"input" | "label" | "root" | "clearTrigger" | "control", import("@zag-js/anatomy").AnatomyPart>;
1
+ export declare const anatomy: import("@zag-js/anatomy").AnatomyInstance<"input" | "label" | "root" | "control" | "clearTrigger">;
2
+ export declare const parts: Record<"input" | "label" | "root" | "control" | "clearTrigger", import("@zag-js/anatomy").AnatomyPart>;
@@ -1,12 +1,22 @@
1
1
  import { getEnvironmentContext } from '../environment-provider/context.svelte.js';
2
+ import { getFieldContext } from '../field/context.svelte.js';
2
3
  import { getLocaleContext } from '../locale-provider/context.svelte.js';
3
4
  import * as pinInput from '@zag-js/pin-input';
4
5
  import { normalizeProps, reflect, useMachine } from '@zag-js/svelte';
5
6
  import { uid } from 'uid';
6
7
  export function createPinInputContext(props) {
8
+ const field = getFieldContext();
7
9
  const locale = getLocaleContext();
8
10
  const environment = getEnvironmentContext();
9
11
  const [state, send] = useMachine(pinInput.machine({
12
+ ids: {
13
+ label: field?.ids.label,
14
+ hiddenInput: field?.ids.control,
15
+ },
16
+ invalid: field?.invalid,
17
+ required: field?.required,
18
+ disabled: field?.disabled,
19
+ readOnly: field?.readOnly,
10
20
  ...props,
11
21
  id: props.id ?? uid(),
12
22
  dir: locale?.dir,
@@ -1,12 +1,21 @@
1
1
  import { getEnvironmentContext } from '../environment-provider/context.svelte.js';
2
+ import { getFieldContext } from '../field/context.svelte.js';
2
3
  import { getLocaleContext } from '../locale-provider/context.svelte.js';
3
4
  import * as ratingGroup from '@zag-js/rating-group';
4
5
  import { normalizeProps, reflect, useMachine } from '@zag-js/svelte';
5
6
  import { uid } from 'uid';
6
7
  export function createRatingGroup(props) {
8
+ const field = getFieldContext();
7
9
  const locale = getLocaleContext();
8
10
  const environment = getEnvironmentContext();
9
11
  const [state, send] = useMachine(ratingGroup.machine({
12
+ ids: {
13
+ label: field?.ids.label,
14
+ hiddenInput: field?.ids.control,
15
+ },
16
+ required: field?.required,
17
+ disabled: field?.disabled,
18
+ readOnly: field?.readOnly,
10
19
  ...props,
11
20
  id: props.id ?? uid(),
12
21
  dir: locale?.dir,
@@ -1,2 +1,2 @@
1
- export declare const anatomy: import("@zag-js/anatomy").Anatomy<"label" | "item" | "root" | "itemText" | "itemControl" | "indicator">;
2
- export declare const parts: Record<"label" | "item" | "root" | "itemText" | "itemControl" | "indicator", import("@zag-js/anatomy").AnatomyPart>;
1
+ export declare const anatomy: import("@zag-js/anatomy").Anatomy<"label" | "root" | "item" | "itemText" | "itemControl" | "indicator">;
2
+ export declare const parts: Record<"label" | "root" | "item" | "itemText" | "itemControl" | "indicator", import("@zag-js/anatomy").AnatomyPart>;
@@ -1,4 +1,5 @@
1
1
  import { getEnvironmentContext } from '../environment-provider/context.svelte.js';
2
+ import { getFieldContext } from '../field/context.svelte.js';
2
3
  import { getLocaleContext } from '../locale-provider/context.svelte.js';
3
4
  import * as select from '@zag-js/select';
4
5
  import { normalizeProps, reflect, useMachine } from '@zag-js/svelte';
@@ -10,9 +11,18 @@ export function createSelect(props) {
10
11
  itemToString: (item) => props.itemToString ? props.itemToString(item) : String(item),
11
12
  isItemDisabled: (item) => props.isItemDisabled ? props.isItemDisabled(item) : false,
12
13
  }));
14
+ const field = getFieldContext();
13
15
  const locale = getLocaleContext();
14
16
  const environment = getEnvironmentContext();
15
17
  const [state, send] = useMachine(select.machine({
18
+ ids: {
19
+ label: field?.ids.label,
20
+ hiddenSelect: field?.ids.control,
21
+ },
22
+ invalid: field?.invalid,
23
+ disabled: field?.disabled,
24
+ readOnly: field?.readOnly,
25
+ required: field?.required,
16
26
  ...props,
17
27
  id: props.id ?? uid(),
18
28
  dir: locale?.dir,
@@ -1,12 +1,22 @@
1
1
  import { getEnvironmentContext } from '../environment-provider/context.svelte.js';
2
+ import { getFieldContext } from '../field/context.svelte.js';
2
3
  import { getLocaleContext } from '../locale-provider/context.svelte.js';
3
4
  import { normalizeProps, reflect, useMachine } from '@zag-js/svelte';
4
5
  import * as switch$ from '@zag-js/switch';
5
6
  import { uid } from 'uid';
6
7
  export function createSwitch(props) {
8
+ const field = getFieldContext();
7
9
  const locale = getLocaleContext();
8
10
  const environment = getEnvironmentContext();
9
11
  const [state, send] = useMachine(switch$.machine({
12
+ ids: {
13
+ label: field?.ids.label,
14
+ hiddenInput: field?.ids.control,
15
+ },
16
+ disabled: field?.disabled,
17
+ readOnly: field?.readOnly,
18
+ invalid: field?.invalid,
19
+ required: field?.required,
10
20
  ...props,
11
21
  id: props.id ?? uid(),
12
22
  dir: locale?.dir,
@@ -1,12 +1,22 @@
1
1
  import { getEnvironmentContext } from '../environment-provider/context.svelte.js';
2
+ import { getFieldContext } from '../field/context.svelte.js';
2
3
  import { getLocaleContext } from '../locale-provider/context.svelte.js';
3
4
  import { normalizeProps, reflect, useMachine } from '@zag-js/svelte';
4
5
  import * as tagsInput from '@zag-js/tags-input';
5
6
  import { uid } from 'uid';
6
7
  export function createTagsInput(props) {
8
+ const field = getFieldContext();
7
9
  const locale = getLocaleContext();
8
10
  const environment = getEnvironmentContext();
9
11
  const [state, send] = useMachine(tagsInput.machine({
12
+ ids: {
13
+ label: field?.ids.label,
14
+ hiddenInput: field?.ids.control,
15
+ },
16
+ invalid: field?.invalid,
17
+ disabled: field?.disabled,
18
+ readOnly: field?.readOnly,
19
+ required: field?.required,
10
20
  ...props,
11
21
  id: props.id ?? uid(),
12
22
  dir: locale?.dir,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "ui-ingredients",
3
3
  "type": "module",
4
- "version": "0.0.31",
4
+ "version": "0.0.32",
5
5
  "packageManager": "pnpm@9.7.0",
6
6
  "svelte": "./dist/index.js",
7
7
  "types": "./dist/index.d.ts",