@sprawlify/react 0.0.1 → 0.0.3

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/dist/index.cjs CHANGED
@@ -1,384 +1,17 @@
1
- //#region rolldown:runtime
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __copyProps = (to, from, except, desc) => {
9
- if (from && typeof from === "object" || typeof from === "function") {
10
- for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
- key = keys[i];
12
- if (!__hasOwnProp.call(to, key) && key !== except) {
13
- __defProp(to, key, {
14
- get: ((k) => from[k]).bind(null, key),
15
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
16
- });
17
- }
18
- }
19
- }
20
- return to;
21
- };
22
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
23
- value: mod,
24
- enumerable: true
25
- }) : target, mod));
26
-
27
- //#endregion
1
+ const require_factory = require('./factory-CfqPG186.cjs');
2
+ const require_create_context = require('./create-context-DCEySQ7J.cjs');
3
+ require('./utils-Cb5K29pi.cjs');
28
4
  let __sprawlify_primitives_core = require("@sprawlify/primitives/core");
29
- let __sprawlify_primitives_utils = require("@sprawlify/primitives/utils");
30
- let react = require("react");
31
- react = __toESM(react);
32
- let react_dom = require("react-dom");
33
- let __sprawlify_primitives_types = require("@sprawlify/primitives/types");
34
- let react_jsx_runtime = require("react/jsx-runtime");
35
-
36
- //#region src/core/use-layout-effect.ts
37
- const useSafeLayoutEffect = typeof globalThis.document !== "undefined" ? react.useLayoutEffect : react.useEffect;
38
-
39
- //#endregion
40
- //#region src/core/bindable.ts
41
- function useBindable(props) {
42
- const initial = props().value ?? props().defaultValue;
43
- const eq = props().isEqual ?? Object.is;
44
- const [initialValue] = (0, react.useState)(initial);
45
- const [value, setValue] = (0, react.useState)(initialValue);
46
- const controlled = props().value !== void 0;
47
- const valueRef = (0, react.useRef)(value);
48
- valueRef.current = controlled ? props().value : value;
49
- const prevValue = (0, react.useRef)(valueRef.current);
50
- useSafeLayoutEffect(() => {
51
- prevValue.current = valueRef.current;
52
- }, [value, props().value]);
53
- const setFn = (value$1) => {
54
- const prev = prevValue.current;
55
- const next = (0, __sprawlify_primitives_utils.isFunction)(value$1) ? value$1(prev) : value$1;
56
- if (props().debug) console.log(`[bindable > ${props().debug}] setValue`, {
57
- next,
58
- prev
59
- });
60
- if (!controlled) setValue(next);
61
- if (!eq(next, prev)) props().onChange?.(next, prev);
62
- };
63
- function get() {
64
- return controlled ? props().value : value;
65
- }
66
- return {
67
- initial: initialValue,
68
- ref: valueRef,
69
- get,
70
- set(value$1) {
71
- (props().sync ? react_dom.flushSync : __sprawlify_primitives_utils.identity)(() => setFn(value$1));
72
- },
73
- invoke(nextValue, prevValue$1) {
74
- props().onChange?.(nextValue, prevValue$1);
75
- },
76
- hash(value$1) {
77
- return props().hash?.(value$1) ?? String(value$1);
78
- }
79
- };
80
- }
81
- useBindable.cleanup = (fn) => {
82
- (0, react.useEffect)(() => fn, []);
83
- };
84
- useBindable.ref = (defaultValue) => {
85
- const value = (0, react.useRef)(defaultValue);
86
- return {
87
- get: () => value.current,
88
- set: (next) => {
89
- value.current = next;
90
- }
91
- };
92
- };
93
-
94
- //#endregion
95
- //#region src/core/refs.ts
96
- function useRefs(refs) {
97
- const ref = (0, react.useRef)(refs);
98
- return {
99
- get(key) {
100
- return ref.current[key];
101
- },
102
- set(key, value) {
103
- ref.current[key] = value;
104
- }
105
- };
106
- }
107
-
108
- //#endregion
109
- //#region src/core/track.ts
110
- const useTrack = (deps, effect) => {
111
- const render = (0, react.useRef)(false);
112
- const called = (0, react.useRef)(false);
113
- (0, react.useEffect)(() => {
114
- if (render.current && called.current) return effect();
115
- called.current = true;
116
- }, [...(deps ?? []).map((d) => typeof d === "function" ? d() : d)]);
117
- (0, react.useEffect)(() => {
118
- render.current = true;
119
- return () => {
120
- render.current = false;
121
- };
122
- }, []);
123
- };
124
-
125
- //#endregion
126
- //#region src/core/machine.ts
127
- function useMachine(machine, userProps = {}) {
128
- const scope = (0, react.useMemo)(() => {
129
- const { id, ids, getRootNode } = userProps;
130
- return (0, __sprawlify_primitives_core.createScope)({
131
- id,
132
- ids,
133
- getRootNode
134
- });
135
- }, [userProps]);
136
- const debug = (...args) => {
137
- if (machine.debug) console.log(...args);
138
- };
139
- const prop = useProp(machine.props?.({
140
- props: (0, __sprawlify_primitives_utils.compact)(userProps),
141
- scope
142
- }) ?? userProps);
143
- const context = machine.context?.({
144
- prop,
145
- bindable: useBindable,
146
- scope,
147
- flush,
148
- getContext() {
149
- return ctx;
150
- },
151
- getComputed() {
152
- return computed;
153
- },
154
- getRefs() {
155
- return refs;
156
- },
157
- getEvent() {
158
- return getEvent();
159
- }
160
- });
161
- const contextRef = useLiveRef(context);
162
- const ctx = {
163
- get(key) {
164
- return contextRef.current?.[key].ref.current;
165
- },
166
- set(key, value) {
167
- contextRef.current?.[key].set(value);
168
- },
169
- initial(key) {
170
- return contextRef.current?.[key].initial;
171
- },
172
- hash(key) {
173
- const current = contextRef.current?.[key].get();
174
- return contextRef.current?.[key].hash(current);
175
- }
176
- };
177
- const effects = (0, react.useRef)(/* @__PURE__ */ new Map());
178
- const transitionRef = (0, react.useRef)(null);
179
- const previousEventRef = (0, react.useRef)(null);
180
- const eventRef = (0, react.useRef)({ type: "" });
181
- const getEvent = () => ({
182
- ...eventRef.current,
183
- current() {
184
- return eventRef.current;
185
- },
186
- previous() {
187
- return previousEventRef.current;
188
- }
189
- });
190
- const getState = () => ({
191
- ...state,
192
- matches(...values) {
193
- return values.includes(state.ref.current);
194
- },
195
- hasTag(tag) {
196
- return !!machine.states[state.ref.current]?.tags?.includes(tag);
197
- }
198
- });
199
- const refs = useRefs(machine.refs?.({
200
- prop,
201
- context: ctx
202
- }) ?? {});
203
- const getParams = () => ({
204
- state: getState(),
205
- context: ctx,
206
- event: getEvent(),
207
- prop,
208
- send,
209
- action,
210
- guard,
211
- track: useTrack,
212
- refs,
213
- computed,
214
- flush,
215
- scope,
216
- choose
217
- });
218
- const action = (keys) => {
219
- const strs = (0, __sprawlify_primitives_utils.isFunction)(keys) ? keys(getParams()) : keys;
220
- if (!strs) return;
221
- const fns = strs.map((s) => {
222
- const fn = machine.implementations?.actions?.[s];
223
- if (!fn) (0, __sprawlify_primitives_utils.warn)(`[sprawlify-js] No implementation found for action "${JSON.stringify(s)}"`);
224
- return fn;
225
- });
226
- for (const fn of fns) fn?.(getParams());
227
- };
228
- const guard = (str) => {
229
- if ((0, __sprawlify_primitives_utils.isFunction)(str)) return str(getParams());
230
- return machine.implementations?.guards?.[str](getParams());
231
- };
232
- const effect = (keys) => {
233
- const strs = (0, __sprawlify_primitives_utils.isFunction)(keys) ? keys(getParams()) : keys;
234
- if (!strs) return;
235
- const fns = strs.map((s) => {
236
- const fn = machine.implementations?.effects?.[s];
237
- if (!fn) (0, __sprawlify_primitives_utils.warn)(`[sprawlify-js] No implementation found for effect "${JSON.stringify(s)}"`);
238
- return fn;
239
- });
240
- const cleanups = [];
241
- for (const fn of fns) {
242
- const cleanup = fn?.(getParams());
243
- if (cleanup) cleanups.push(cleanup);
244
- }
245
- return () => cleanups.forEach((fn) => fn?.());
246
- };
247
- const choose = (transitions) => {
248
- return (0, __sprawlify_primitives_utils.toArray)(transitions).find((t) => {
249
- let result = !t.guard;
250
- if ((0, __sprawlify_primitives_utils.isString)(t.guard)) result = !!guard(t.guard);
251
- else if ((0, __sprawlify_primitives_utils.isFunction)(t.guard)) result = t.guard(getParams());
252
- return result;
253
- });
254
- };
255
- const computed = (key) => {
256
- (0, __sprawlify_primitives_utils.ensure)(machine.computed, () => `[sprawlify-js] No computed object found on machine`);
257
- const fn = machine.computed[key];
258
- return fn({
259
- context: ctx,
260
- event: getEvent(),
261
- prop,
262
- refs,
263
- scope,
264
- computed
265
- });
266
- };
267
- const state = useBindable(() => ({
268
- defaultValue: machine.initialState({ prop }),
269
- onChange(nextState, prevState) {
270
- if (prevState) {
271
- effects.current.get(prevState)?.();
272
- effects.current.delete(prevState);
273
- }
274
- if (prevState) action(machine.states[prevState]?.exit);
275
- action(transitionRef.current?.actions);
276
- const cleanup = effect(machine.states[nextState]?.effects);
277
- if (cleanup) effects.current.set(nextState, cleanup);
278
- if (prevState === __sprawlify_primitives_core.INIT_STATE) {
279
- action(machine.entry);
280
- const cleanup$1 = effect(machine.effects);
281
- if (cleanup$1) effects.current.set(__sprawlify_primitives_core.INIT_STATE, cleanup$1);
282
- }
283
- action(machine.states[nextState]?.entry);
284
- }
285
- }));
286
- const hydratedStateRef = (0, react.useRef)(void 0);
287
- const statusRef = (0, react.useRef)(__sprawlify_primitives_core.MachineStatus.NotStarted);
288
- useSafeLayoutEffect(() => {
289
- queueMicrotask(() => {
290
- const started = statusRef.current === __sprawlify_primitives_core.MachineStatus.Started;
291
- statusRef.current = __sprawlify_primitives_core.MachineStatus.Started;
292
- debug(started ? "rehydrating..." : "initializing...");
293
- const initialState = hydratedStateRef.current ?? state.initial;
294
- state.invoke(initialState, started ? state.get() : __sprawlify_primitives_core.INIT_STATE);
295
- });
296
- const fns = effects.current;
297
- const currentState = state.ref.current;
298
- return () => {
299
- debug("unmounting...");
300
- hydratedStateRef.current = currentState;
301
- statusRef.current = __sprawlify_primitives_core.MachineStatus.Stopped;
302
- fns.forEach((fn) => fn?.());
303
- effects.current = /* @__PURE__ */ new Map();
304
- transitionRef.current = null;
305
- queueMicrotask(() => {
306
- action(machine.exit);
307
- });
308
- };
309
- }, []);
310
- const getCurrentState = () => {
311
- if ("ref" in state) return state.ref.current;
312
- return state.get();
313
- };
314
- const send = (event) => {
315
- queueMicrotask(() => {
316
- if (statusRef.current !== __sprawlify_primitives_core.MachineStatus.Started) return;
317
- previousEventRef.current = eventRef.current;
318
- eventRef.current = event;
319
- const currentState = getCurrentState();
320
- const transition = choose(machine.states[currentState].on?.[event.type] ?? machine.on?.[event.type]);
321
- if (!transition) return;
322
- transitionRef.current = transition;
323
- const target = transition.target ?? currentState;
324
- debug("transition", event.type, transition.target || currentState, `(${transition.actions})`);
325
- const changed = target !== currentState;
326
- if (changed) (0, react_dom.flushSync)(() => state.set(target));
327
- else if (transition.reenter && !changed) state.invoke(currentState, currentState);
328
- else action(transition.actions ?? []);
329
- });
330
- };
331
- machine.watch?.(getParams());
332
- return {
333
- state: getState(),
334
- send,
335
- context: ctx,
336
- prop,
337
- scope,
338
- refs,
339
- computed,
340
- event: getEvent(),
341
- getStatus: () => statusRef.current
342
- };
343
- }
344
- function useLiveRef(value) {
345
- const ref = (0, react.useRef)(value);
346
- ref.current = value;
347
- return ref;
348
- }
349
- function useProp(value) {
350
- const ref = useLiveRef(value);
351
- return function get(key) {
352
- return ref.current[key];
353
- };
354
- }
355
- function flush(fn) {
356
- queueMicrotask(() => {
357
- (0, react_dom.flushSync)(() => fn());
358
- });
359
- }
360
-
361
- //#endregion
362
- //#region src/core/normalize-props.ts
363
- const normalizeProps = (0, __sprawlify_primitives_types.createNormalizer)((v) => v);
364
-
365
- //#endregion
366
- //#region src/core/portal.tsx
367
- const Portal = (props) => {
368
- const { children, container, disabled, getRootNode } = props;
369
- if (typeof window === "undefined" || disabled) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react.Fragment, { children });
370
- const doc = getRootNode?.().ownerDocument ?? document;
371
- const mountNode = container?.current ?? doc.body;
372
- return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react.Fragment, { children: react.Children.map(children, (child) => (0, react_dom.createPortal)(child, mountNode)) });
373
- };
374
5
 
375
- //#endregion
376
- exports.Portal = Portal;
377
- Object.defineProperty(exports, 'mergeProps', {
378
- enumerable: true,
379
- get: function () {
380
- return __sprawlify_primitives_core.mergeProps;
381
- }
382
- });
383
- exports.normalizeProps = normalizeProps;
384
- exports.useMachine = useMachine;
6
+ exports.EnvironmentProvider = require_factory.EnvironmentProvider;
7
+ exports.LocaleProvider = require_factory.LocaleProvider;
8
+ exports.Portal = require_factory.Portal;
9
+ exports.createContext = require_create_context.createContext;
10
+ exports.jsxFactory = require_factory.jsxFactory;
11
+ exports.normalizeProps = require_factory.normalizeProps;
12
+ exports.sprawlify = require_factory.sprawlify;
13
+ exports.useCollator = require_factory.useCollator;
14
+ exports.useEnvironmentContext = require_factory.useEnvironmentContext;
15
+ exports.useFilter = require_factory.useFilter;
16
+ exports.useLocaleContext = require_factory.useLocaleContext;
17
+ exports.useMachine = require_factory.useMachine;
package/dist/index.d.cts CHANGED
@@ -1,19 +1,13 @@
1
- import { Machine, MachineSchema, Service, mergeProps } from "@sprawlify/primitives/core";
2
- import * as _sprawlify_primitives_types0 from "@sprawlify/primitives/types";
3
- import { CSSProperties, HTMLAttributes, JSX, PropsWithChildren, RefObject } from "react";
1
+ import { a as HTMLProps, c as jsxFactory, i as normalizeProps, l as sprawlify, n as Optional, o as HTMLSprawlifyProps, r as PropTypes, s as PolymorphicProps, t as Assign } from "./types-B_h5HVp2.cjs";
2
+ import { n as createContext } from "./index-BbLlW5Mk.cjs";
3
+ import { JSX, PropsWithChildren, ReactNode, RefObject } from "react";
4
+ import { Machine, MachineSchema, Service } from "@sprawlify/primitives/core";
5
+ import * as react_jsx_runtime0 from "react/jsx-runtime";
6
+ import { FilterOptions, FilterReturn, Locale } from "@sprawlify/primitives/i18n-utils";
4
7
 
5
8
  //#region src/core/machine.d.ts
6
9
  declare function useMachine<T extends MachineSchema>(machine: Machine<T>, userProps?: Partial<T["props"]>): Service<T>;
7
10
  //#endregion
8
- //#region src/core/normalize-props.d.ts
9
- type WithoutRef<T> = Omit<T, "ref">;
10
- type ElementsWithoutRef = { [K in keyof JSX.IntrinsicElements]: WithoutRef<JSX.IntrinsicElements[K]> };
11
- type PropTypes = ElementsWithoutRef & {
12
- element: WithoutRef<HTMLAttributes<HTMLElement>>;
13
- style: CSSProperties;
14
- };
15
- declare const normalizeProps: _sprawlify_primitives_types0.NormalizeProps<PropTypes>;
16
- //#endregion
17
11
  //#region src/core/portal.d.ts
18
12
  interface PortalProps {
19
13
  disabled?: boolean | undefined;
@@ -22,4 +16,39 @@ interface PortalProps {
22
16
  }
23
17
  declare const Portal: (props: PropsWithChildren<PortalProps>) => JSX.Element;
24
18
  //#endregion
25
- export { Portal, PortalProps, PropTypes, mergeProps, normalizeProps, useMachine };
19
+ //#region src/providers/environment/use-environment-context.d.ts
20
+ type RootNode = ShadowRoot | Document | Node;
21
+ interface UseEnvironmentContext {
22
+ getRootNode(): RootNode;
23
+ getDocument(): Document;
24
+ getWindow(): Window & typeof globalThis;
25
+ }
26
+ //#endregion
27
+ //#region src/providers/environment/environment-provider.d.ts
28
+ interface EnvironmentProviderProps {
29
+ children?: ReactNode | undefined;
30
+ value?: RootNode | (() => RootNode) | undefined;
31
+ }
32
+ declare const EnvironmentProvider: (props: EnvironmentProviderProps) => react_jsx_runtime0.JSX.Element;
33
+ //#endregion
34
+ //#region src/providers/locale/locale-provider.d.ts
35
+ interface LocaleProviderProps extends PropsWithChildren {
36
+ locale: string;
37
+ }
38
+ declare const LocaleProvider: (props: LocaleProviderProps) => react_jsx_runtime0.JSX.Element;
39
+ //#endregion
40
+ //#region src/providers/locale/use-collator.d.ts
41
+ interface UseCollatorProps extends Intl.CollatorOptions {
42
+ locale?: string;
43
+ }
44
+ declare function useCollator(props?: UseCollatorProps): Intl.Collator;
45
+ //#endregion
46
+ //#region src/providers/locale/use-filter.d.ts
47
+ interface UseFilterProps extends FilterOptions {}
48
+ declare function useFilter(props: UseFilterProps): UseFilterReturn;
49
+ interface UseFilterReturn extends FilterReturn {}
50
+ //#endregion
51
+ //#region src/providers/locale/use-locale-context.d.ts
52
+ interface UseLocaleContext extends Locale {}
53
+ //#endregion
54
+ export { type Assign, EnvironmentProvider, EnvironmentProviderProps, HTMLProps, HTMLSprawlifyProps, LocaleProvider, LocaleProviderProps, type Optional, PolymorphicProps, Portal, PortalProps, PropTypes, RootNode, UseCollatorProps, UseEnvironmentContext, UseFilterProps, UseFilterReturn, UseLocaleContext, createContext, jsxFactory, normalizeProps, sprawlify, useCollator, useEnvironmentContext, useFilter, useLocaleContext, useMachine };
package/dist/index.d.mts CHANGED
@@ -1,25 +1,42 @@
1
- import { Machine, MachineSchema, Service, mergeProps } from "@sprawlify/primitives/core";
2
- import { CSSProperties, HTMLAttributes, JSX, PropsWithChildren, RefObject } from "react";
3
- import * as _sprawlify_primitives_types0 from "@sprawlify/primitives/types";
1
+ import { a as PropTypes, c as HTMLProps, d as jsxFactory, f as sprawlify, i as PortalProps, l as HTMLSprawlifyProps, n as Optional, o as normalizeProps, r as Portal, s as useMachine, t as Assign, u as PolymorphicProps } from "./types-CwELsIN6.mjs";
2
+ import { n as createContext } from "./index-CgR7RZbW.mjs";
3
+ import { PropsWithChildren, ReactNode } from "react";
4
+ import * as react_jsx_runtime0 from "react/jsx-runtime";
5
+ import { FilterOptions, FilterReturn, Locale } from "@sprawlify/primitives/i18n-utils";
4
6
 
5
- //#region src/core/machine.d.ts
6
- declare function useMachine<T extends MachineSchema>(machine: Machine<T>, userProps?: Partial<T["props"]>): Service<T>;
7
+ //#region src/providers/environment/use-environment-context.d.ts
8
+ type RootNode = ShadowRoot | Document | Node;
9
+ interface UseEnvironmentContext {
10
+ getRootNode(): RootNode;
11
+ getDocument(): Document;
12
+ getWindow(): Window & typeof globalThis;
13
+ }
14
+ //#endregion
15
+ //#region src/providers/environment/environment-provider.d.ts
16
+ interface EnvironmentProviderProps {
17
+ children?: ReactNode | undefined;
18
+ value?: RootNode | (() => RootNode) | undefined;
19
+ }
20
+ declare const EnvironmentProvider: (props: EnvironmentProviderProps) => react_jsx_runtime0.JSX.Element;
7
21
  //#endregion
8
- //#region src/core/normalize-props.d.ts
9
- type WithoutRef<T> = Omit<T, "ref">;
10
- type ElementsWithoutRef = { [K in keyof JSX.IntrinsicElements]: WithoutRef<JSX.IntrinsicElements[K]> };
11
- type PropTypes = ElementsWithoutRef & {
12
- element: WithoutRef<HTMLAttributes<HTMLElement>>;
13
- style: CSSProperties;
14
- };
15
- declare const normalizeProps: _sprawlify_primitives_types0.NormalizeProps<PropTypes>;
22
+ //#region src/providers/locale/locale-provider.d.ts
23
+ interface LocaleProviderProps extends PropsWithChildren {
24
+ locale: string;
25
+ }
26
+ declare const LocaleProvider: (props: LocaleProviderProps) => react_jsx_runtime0.JSX.Element;
16
27
  //#endregion
17
- //#region src/core/portal.d.ts
18
- interface PortalProps {
19
- disabled?: boolean | undefined;
20
- container?: RefObject<HTMLElement> | undefined;
21
- getRootNode?: (() => ShadowRoot | Document | Node) | undefined;
28
+ //#region src/providers/locale/use-collator.d.ts
29
+ interface UseCollatorProps extends Intl.CollatorOptions {
30
+ locale?: string;
22
31
  }
23
- declare const Portal: (props: PropsWithChildren<PortalProps>) => JSX.Element;
32
+ declare function useCollator(props?: UseCollatorProps): Intl.Collator;
33
+ //#endregion
34
+ //#region src/providers/locale/use-filter.d.ts
35
+ interface UseFilterProps extends FilterOptions {}
36
+ declare function useFilter(props: UseFilterProps): UseFilterReturn;
37
+ interface UseFilterReturn extends FilterReturn {}
38
+ //#endregion
39
+ //#region src/providers/locale/use-locale-context.d.ts
40
+ interface UseLocaleContext extends Locale {}
24
41
  //#endregion
25
- export { Portal, PortalProps, PropTypes, mergeProps, normalizeProps, useMachine };
42
+ export { type Assign, EnvironmentProvider, type EnvironmentProviderProps, HTMLProps, HTMLSprawlifyProps, LocaleProvider, type LocaleProviderProps, type Optional, PolymorphicProps, Portal, PortalProps, PropTypes, type RootNode, type UseCollatorProps, type UseEnvironmentContext, type UseFilterProps, type UseFilterReturn, type UseLocaleContext, createContext, jsxFactory, normalizeProps, sprawlify, useCollator, useEnvironmentContext, useFilter, useLocaleContext, useMachine };