@qds.dev/base 0.11.2 → 0.13.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.
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ComputedSignal, Signal } from "@qwik.dev/core";
|
|
2
|
+
type SignalFields<T> = {
|
|
3
|
+
[K in keyof T]: T[K] extends Signal ? K : never;
|
|
4
|
+
}[keyof T];
|
|
5
|
+
type UnwrapSignal<T> = T extends Signal<infer V> ? V : T;
|
|
6
|
+
/**
|
|
7
|
+
* Creates a type-safe getter factory for exposing component context state to consumers.
|
|
8
|
+
*
|
|
9
|
+
* Qwik's context system requires `useContext()` inside a `component$()` that is a
|
|
10
|
+
* descendant of the provider. Consumers can't read component state from the parent scope.
|
|
11
|
+
* The QDS UI plugin (`qds()`) solves this by detecting `get`-prefixed namespace properties
|
|
12
|
+
* in JSX and generating `component$()` wrappers at build time.
|
|
13
|
+
*
|
|
14
|
+
* This function creates proxy placeholders typed against the context. The plugin rewrites
|
|
15
|
+
* all references before runtime — the proxy only triggers if the plugin is missing.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* // select/index.ts
|
|
20
|
+
* const context = createContextProxy<SelectContext>();
|
|
21
|
+
*
|
|
22
|
+
* export const getIsOpen = context("isOpen");
|
|
23
|
+
* export const getSelectedValues = context("selectedValues");
|
|
24
|
+
*
|
|
25
|
+
* // Consumer JSX — plugin transforms this into a generated component$():
|
|
26
|
+
* <select.trigger>{select.getIsOpen.value ? "Open" : "Closed"}</select.trigger>
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare function createContextProxy<TContext>(): <TField extends SignalFields<TContext>>(field: TField) => ComputedSignal<UnwrapSignal<TContext[TField]>>;
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
//#region src/helpers/create-getter.ts
|
|
2
|
+
/**
|
|
3
|
+
* Creates a type-safe getter factory for exposing component context state to consumers.
|
|
4
|
+
*
|
|
5
|
+
* Qwik's context system requires `useContext()` inside a `component$()` that is a
|
|
6
|
+
* descendant of the provider. Consumers can't read component state from the parent scope.
|
|
7
|
+
* The QDS UI plugin (`qds()`) solves this by detecting `get`-prefixed namespace properties
|
|
8
|
+
* in JSX and generating `component$()` wrappers at build time.
|
|
9
|
+
*
|
|
10
|
+
* This function creates proxy placeholders typed against the context. The plugin rewrites
|
|
11
|
+
* all references before runtime — the proxy only triggers if the plugin is missing.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* // select/index.ts
|
|
16
|
+
* const context = createContextProxy<SelectContext>();
|
|
17
|
+
*
|
|
18
|
+
* export const getIsOpen = context("isOpen");
|
|
19
|
+
* export const getSelectedValues = context("selectedValues");
|
|
20
|
+
*
|
|
21
|
+
* // Consumer JSX — plugin transforms this into a generated component$():
|
|
22
|
+
* <select.trigger>{select.getIsOpen.value ? "Open" : "Closed"}</select.trigger>
|
|
23
|
+
* ```
|
|
24
|
+
*/ function createContextProxy() {
|
|
25
|
+
return (field) => {
|
|
26
|
+
return new Proxy({}, { get() {
|
|
27
|
+
throw new Error(`We tried to grab and create context for ${String(field)} in the top level, using the getX pattern, but it appears the qds vite plugin did not transform it correct (or is missing). Add qds() to the start of the plugins array in your Vite config.`);
|
|
28
|
+
} });
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
//#endregion
|
|
33
|
+
export { createContextProxy };
|
package/lib/helpers/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { appendId, removeId, toggleId } from "./aria-ids.qwik.mjs";
|
|
2
|
+
import { createContextProxy } from "./create-getter.qwik.mjs";
|
|
2
3
|
import { createCache } from "./cache.qwik.mjs";
|
|
3
4
|
import { hashString } from "./hash.qwik.mjs";
|
|
4
5
|
import { hasIdInList } from "./id-list.qwik.mjs";
|
|
@@ -6,4 +7,4 @@ import { getIndexFromValue, getValueFromIndex, registerItem } from "./item-regis
|
|
|
6
7
|
import { getNextEnabledIndex, getPrevEnabledIndex } from "./list-navigation.qwik.mjs";
|
|
7
8
|
import { createSymbolName } from "./symbol.qwik.mjs";
|
|
8
9
|
|
|
9
|
-
export { appendId, createCache, createSymbolName, getIndexFromValue, getNextEnabledIndex, getPrevEnabledIndex, getValueFromIndex, hasIdInList, hashString, registerItem, removeId, toggleId };
|
|
10
|
+
export { appendId, createCache, createContextProxy, createSymbolName, getIndexFromValue, getNextEnabledIndex, getPrevEnabledIndex, getValueFromIndex, hasIdInList, hashString, registerItem, removeId, toggleId };
|
package/lib/index.qwik.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { appendId, removeId, toggleId } from "./helpers/aria-ids.qwik.mjs";
|
|
2
|
+
import { createContextProxy } from "./helpers/create-getter.qwik.mjs";
|
|
2
3
|
import { createCache } from "./helpers/cache.qwik.mjs";
|
|
3
4
|
import { hashString } from "./helpers/hash.qwik.mjs";
|
|
4
5
|
import { hasIdInList } from "./helpers/id-list.qwik.mjs";
|
|
@@ -18,4 +19,4 @@ import { useStoreSignal } from "./state/store-signal.qwik.mjs";
|
|
|
18
19
|
import "./state/index.qwik.mjs";
|
|
19
20
|
import { addWindowEventListener, removeWindowEventListener } from "./types/window-events.qwik.mjs";
|
|
20
21
|
|
|
21
|
-
export { addWindowEventListener, appendId, createCache, createSymbolName, destructureBindings, getIndexFromValue, getNextEnabledIndex, getPrevEnabledIndex, getValueFromIndex, hasIdInList, hashString, livePropsContextId, mergeRefs, registerItem, removeId, removeWindowEventListener, toggleId, useBindings, useBoundSignal, useDebouncer, useMountTask$, useMountTaskQrl, usePlayground, useResumed$, useResumedQrl, useStoreSignal };
|
|
22
|
+
export { addWindowEventListener, appendId, createCache, createContextProxy, createSymbolName, destructureBindings, getIndexFromValue, getNextEnabledIndex, getPrevEnabledIndex, getValueFromIndex, hasIdInList, hashString, livePropsContextId, mergeRefs, registerItem, removeId, removeWindowEventListener, toggleId, useBindings, useBoundSignal, useDebouncer, useMountTask$, useMountTaskQrl, usePlayground, useResumed$, useResumedQrl, useStoreSignal };
|