@kirana-ui/react 0.0.0 → 0.1.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/LICENSE +1 -1
- package/package.json +121 -6
- package/src/accordion.d.ts +28 -0
- package/src/accordion.js +81 -0
- package/src/accordion.js.map +1 -0
- package/src/alert-dialog.d.ts +2 -0
- package/src/alert-dialog.js +1 -0
- package/src/avatar.d.ts +16 -0
- package/src/avatar.js +42 -0
- package/src/avatar.js.map +1 -0
- package/src/button.d.ts +7 -0
- package/src/button.js +15 -0
- package/src/button.js.map +1 -0
- package/src/checkbox.d.ts +18 -0
- package/src/checkbox.js +50 -0
- package/src/checkbox.js.map +1 -0
- package/src/collapsible.d.ts +20 -0
- package/src/collapsible.js +43 -0
- package/src/collapsible.js.map +1 -0
- package/src/dialog.d.ts +46 -0
- package/src/dialog.js +53 -0
- package/src/dialog.js.map +1 -0
- package/src/direction-context.d.ts +4 -0
- package/src/direction-context.js +7 -0
- package/src/direction-context.js.map +1 -0
- package/src/direction.d.ts +15 -0
- package/src/direction.js +27 -0
- package/src/direction.js.map +1 -0
- package/src/field-context.d.ts +4 -0
- package/src/field-context.js +7 -0
- package/src/field-context.js.map +1 -0
- package/src/field.d.ts +31 -0
- package/src/field.js +71 -0
- package/src/field.js.map +1 -0
- package/src/fieldset.d.ts +15 -0
- package/src/fieldset.js +31 -0
- package/src/fieldset.js.map +1 -0
- package/src/form.d.ts +8 -0
- package/src/form.js +27 -0
- package/src/form.js.map +1 -0
- package/src/hover-card.d.ts +13 -0
- package/src/hover-card.js +7 -0
- package/src/hover-card.js.map +1 -0
- package/src/hover-popup.d.ts +21 -0
- package/src/hover-popup.js +49 -0
- package/src/hover-popup.js.map +1 -0
- package/src/index.d.ts +24 -0
- package/src/index.js +23 -0
- package/src/meter.d.ts +13 -0
- package/src/meter.js +7 -0
- package/src/meter.js.map +1 -0
- package/src/popover.d.ts +31 -0
- package/src/popover.js +51 -0
- package/src/popover.js.map +1 -0
- package/src/progress.d.ts +13 -0
- package/src/progress.js +7 -0
- package/src/progress.js.map +1 -0
- package/src/radio-group.d.ts +24 -0
- package/src/radio-group.js +86 -0
- package/src/radio-group.js.map +1 -0
- package/src/runtime.d.ts +34 -0
- package/src/runtime.js +120 -0
- package/src/runtime.js.map +1 -0
- package/src/select.d.ts +46 -0
- package/src/select.js +91 -0
- package/src/select.js.map +1 -0
- package/src/separator.d.ts +6 -0
- package/src/separator.js +10 -0
- package/src/separator.js.map +1 -0
- package/src/slider.d.ts +29 -0
- package/src/slider.js +82 -0
- package/src/slider.js.map +1 -0
- package/src/switch.d.ts +14 -0
- package/src/switch.js +45 -0
- package/src/switch.js.map +1 -0
- package/src/tabs.d.ts +30 -0
- package/src/tabs.js +73 -0
- package/src/tabs.js.map +1 -0
- package/src/toggle.d.ts +6 -0
- package/src/toggle.js +15 -0
- package/src/toggle.js.map +1 -0
- package/src/tooltip.d.ts +13 -0
- package/src/tooltip.js +7 -0
- package/src/tooltip.js.map +1 -0
- package/src/value-bar.d.ts +17 -0
- package/src/value-bar.js +51 -0
- package/src/value-bar.js.map +1 -0
- package/README.md +0 -7
- package/index.js +0 -5
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { PartComponentProps } from "./runtime.js";
|
|
2
|
+
import { ReactElement } from "react";
|
|
3
|
+
import { RadioGroupProps } from "@kirana-ui/core/radio-group";
|
|
4
|
+
export type RootProps<Value> = Omit<RadioGroupProps<Value>, "id"> & {
|
|
5
|
+
id?: string;
|
|
6
|
+
} & PartComponentProps;
|
|
7
|
+
declare function Root<Value>({ value, defaultValue, onValueChange, disabled, readOnly, required, name, id, ...rest }: RootProps<Value>): ReactElement;
|
|
8
|
+
export interface ItemProps extends PartComponentProps {
|
|
9
|
+
value: unknown;
|
|
10
|
+
disabled?: boolean;
|
|
11
|
+
}
|
|
12
|
+
declare function Item({ value, disabled, children, ...rest }: ItemProps): ReactElement;
|
|
13
|
+
export interface IndicatorProps extends PartComponentProps {
|
|
14
|
+
/** Keeps the mark in the page while its item is not the chosen one. */
|
|
15
|
+
keepMounted?: boolean;
|
|
16
|
+
}
|
|
17
|
+
declare function Indicator({ keepMounted, ...props }: IndicatorProps): ReactElement | null;
|
|
18
|
+
export declare const RadioGroup: {
|
|
19
|
+
Root: typeof Root;
|
|
20
|
+
Item: typeof Item;
|
|
21
|
+
Indicator: typeof Indicator;
|
|
22
|
+
};
|
|
23
|
+
export {};
|
|
24
|
+
//# sourceMappingURL=radio-group.d.ts.map
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { DirectionContext } from "./direction-context.js";
|
|
2
|
+
import { FieldContext } from "./field-context.js";
|
|
3
|
+
import { useController, useOptions, usePart, useSignal } from "./runtime.js";
|
|
4
|
+
import { defined, within } from "@kirana-ui/core";
|
|
5
|
+
import { createContext, useContext, useEffect, useMemo } from "react";
|
|
6
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
7
|
+
import { createRadioGroup } from "@kirana-ui/core/radio-group";
|
|
8
|
+
// src/radio-group.tsx
|
|
9
|
+
const Context = createContext(null);
|
|
10
|
+
const ItemContext = createContext(void 0);
|
|
11
|
+
function useRadioGroup(part) {
|
|
12
|
+
const controller = useContext(Context);
|
|
13
|
+
if (!controller) throw new Error(`<RadioGroup.${part}> must be inside <RadioGroup.Root>`);
|
|
14
|
+
return controller;
|
|
15
|
+
}
|
|
16
|
+
function Root({ value, defaultValue, onValueChange, disabled, readOnly, required, name, id, ...rest }) {
|
|
17
|
+
const field = useContext(FieldContext) ?? void 0;
|
|
18
|
+
const direction = useContext(DirectionContext) ?? void 0;
|
|
19
|
+
const create = within(createRadioGroup, {
|
|
20
|
+
field,
|
|
21
|
+
direction
|
|
22
|
+
});
|
|
23
|
+
const controller = useController(create, {
|
|
24
|
+
value,
|
|
25
|
+
defaultValue,
|
|
26
|
+
onValueChange,
|
|
27
|
+
disabled,
|
|
28
|
+
readOnly,
|
|
29
|
+
required,
|
|
30
|
+
name,
|
|
31
|
+
id
|
|
32
|
+
});
|
|
33
|
+
return /* @__PURE__ */ jsx(Context.Provider, {
|
|
34
|
+
value: controller,
|
|
35
|
+
children: /* @__PURE__ */ jsx(RootElement, { ...rest })
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
const RootElement = (props) => usePart(useRadioGroup("Root").root, "div", props);
|
|
39
|
+
function Item({ value, disabled, children, ...rest }) {
|
|
40
|
+
const controller = useRadioGroup("Item");
|
|
41
|
+
const options = useOptions({ disabled });
|
|
42
|
+
const parts = useMemo(() => {
|
|
43
|
+
const read = () => defined(options.get());
|
|
44
|
+
return {
|
|
45
|
+
item: controller.item(value, read),
|
|
46
|
+
input: controller.itemInput(value, read)
|
|
47
|
+
};
|
|
48
|
+
}, [
|
|
49
|
+
controller,
|
|
50
|
+
value,
|
|
51
|
+
options
|
|
52
|
+
]);
|
|
53
|
+
return /* @__PURE__ */ jsx(ItemContext.Provider, {
|
|
54
|
+
value,
|
|
55
|
+
children: /* @__PURE__ */ jsxs(PartElement, {
|
|
56
|
+
part: parts.item,
|
|
57
|
+
tag: "span",
|
|
58
|
+
...rest,
|
|
59
|
+
children: [children, /* @__PURE__ */ jsx(PartElement, {
|
|
60
|
+
part: parts.input,
|
|
61
|
+
tag: "input"
|
|
62
|
+
})]
|
|
63
|
+
})
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
const PartElement = ({ part, tag, ...props }) => usePart(part, tag, props);
|
|
67
|
+
function Indicator({ keepMounted, ...props }) {
|
|
68
|
+
const controller = useRadioGroup("Indicator");
|
|
69
|
+
const value = useContext(ItemContext);
|
|
70
|
+
const indicator = useMemo(() => controller.indicator(value), [controller, value]);
|
|
71
|
+
useEffect(() => indicator.mount(), [indicator]);
|
|
72
|
+
if (!useSignal(indicator.mounted) && !keepMounted) return null;
|
|
73
|
+
return /* @__PURE__ */ jsx(PartElement, {
|
|
74
|
+
part: indicator.part,
|
|
75
|
+
tag: "span",
|
|
76
|
+
...props
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
const RadioGroup = {
|
|
80
|
+
Root,
|
|
81
|
+
Item,
|
|
82
|
+
Indicator
|
|
83
|
+
};
|
|
84
|
+
export { RadioGroup };
|
|
85
|
+
|
|
86
|
+
//# sourceMappingURL=radio-group.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"radio-group.js","names":[],"sources":["../../../../packages/react/src/radio-group.tsx"],"sourcesContent":["import { defined, within } from \"@kirana-ui/core\"\nimport type { Part } from \"@kirana-ui/core\"\nimport { createRadioGroup } from \"@kirana-ui/core/radio-group\"\nimport type { RadioGroup as Controller, RadioGroupProps } from \"@kirana-ui/core/radio-group\"\nimport { createContext, useContext, useEffect, useMemo } from \"react\"\nimport type { ReactElement } from \"react\"\n\nimport { DirectionContext } from \"./direction-context.ts\"\nimport { FieldContext } from \"./field-context.ts\"\nimport { useController, useOptions, usePart, useSignal } from \"./runtime.tsx\"\nimport type { PartComponentProps } from \"./runtime.tsx\"\n\nconst Context = createContext<Controller | null>(null)\nconst ItemContext = createContext<unknown>(undefined)\n\nfunction useRadioGroup(part: string): Controller {\n const controller = useContext(Context)\n if (!controller) throw new Error(`<RadioGroup.${part}> must be inside <RadioGroup.Root>`)\n return controller\n}\n\nexport type RootProps<Value> = Omit<RadioGroupProps<Value>, \"id\"> & {\n id?: string\n} & PartComponentProps\n\nfunction Root<Value>({\n value,\n defaultValue,\n onValueChange,\n disabled,\n readOnly,\n required,\n name,\n id,\n ...rest\n}: RootProps<Value>): ReactElement {\n const field = useContext(FieldContext) ?? undefined\n const direction = useContext(DirectionContext) ?? undefined\n const create = within(createRadioGroup<Value>, { field, direction })\n const controller = useController(create, {\n value: value as Value | null | undefined,\n defaultValue: defaultValue as Value | null | undefined,\n onValueChange,\n disabled,\n readOnly,\n required,\n name,\n id,\n }) as unknown as Controller\n return (\n <Context.Provider value={controller}>\n <RootElement {...rest} />\n </Context.Provider>\n )\n}\n\nconst RootElement = (props: PartComponentProps) => usePart(useRadioGroup(\"Root\").root, \"div\", props)\n\nexport interface ItemProps extends PartComponentProps {\n value: unknown\n disabled?: boolean\n}\n\nfunction Item({ value, disabled, children, ...rest }: ItemProps): ReactElement {\n const controller = useRadioGroup(\"Item\")\n const options = useOptions({ disabled })\n const parts = useMemo(() => {\n const read = () => defined(options.get())\n return { item: controller.item(value, read), input: controller.itemInput(value, read) }\n }, [controller, value, options])\n return (\n <ItemContext.Provider value={value}>\n <PartElement part={parts.item} tag=\"span\" {...rest}>\n {children}\n <PartElement part={parts.input} tag=\"input\" />\n </PartElement>\n </ItemContext.Provider>\n )\n}\n\nconst PartElement = ({ part, tag, ...props }: PartComponentProps & { part: Part; tag: string }) =>\n usePart(part, tag, props)\n\nexport interface IndicatorProps extends PartComponentProps {\n /** Keeps the mark in the page while its item is not the chosen one. */\n keepMounted?: boolean\n}\n\nfunction Indicator({ keepMounted, ...props }: IndicatorProps): ReactElement | null {\n const controller = useRadioGroup(\"Indicator\")\n const value = useContext(ItemContext)\n const indicator = useMemo(() => controller.indicator(value), [controller, value])\n useEffect(() => indicator.mount(), [indicator])\n const mounted = useSignal(indicator.mounted)\n if (!mounted && !keepMounted) return null\n return <PartElement part={indicator.part} tag=\"span\" {...props} />\n}\n\nexport const RadioGroup = { Root, Item, Indicator }\n"],"mappings":";;;;;;;;AAYA,MAAM,UAAU,cAAiC,IAAI;AACrD,MAAM,cAAc,cAAuB,KAAA,CAAS;AAEpD,SAAS,cAAc,MAA0B;CAC/C,MAAM,aAAa,WAAW,OAAO;CACrC,IAAI,CAAC,YAAY,MAAM,IAAI,MAAM,eAAe,KAAK,mCAAmC;CACxF,OAAO;AACT;AAMA,SAAS,KAAY,EACnB,OACA,cACA,eACA,UACA,UACA,UACA,MACA,IACA,GAAG,QAC8B;CACjC,MAAM,QAAQ,WAAW,YAAY,KAAK,KAAA;CAC1C,MAAM,YAAY,WAAW,gBAAgB,KAAK,KAAA;CAClD,MAAM,SAAS,OAAO,kBAAyB;EAAE;EAA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EAAM,KAAI;EAAO,GAAI;CAAQ,CAAA;AACnE;AAEA,MAAa,aAAa;CAAE;CAAM;CAAM;AAAU"}
|
package/src/runtime.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { Part, ReadonlySignal } from "@kirana-ui/core";
|
|
2
|
+
import { ReactElement, ReactNode, Ref } from "react";
|
|
3
|
+
/** A core signal's value, re-rendering when it changes. */
|
|
4
|
+
export declare function useSignal<T>(source: ReadonlySignal<T>): T;
|
|
5
|
+
/**
|
|
6
|
+
* A component's controller, made once, following the props of each render. The props reach the
|
|
7
|
+
* core after the render commits (a layout effect), never during it: another part that reads the
|
|
8
|
+
* controller is another component, and React forbids updating it mid-render.
|
|
9
|
+
*/
|
|
10
|
+
export declare function useController<P extends object, C extends {
|
|
11
|
+
mount(): () => void;
|
|
12
|
+
}>(create: (props: ReadonlySignal<P>) => C, props: Omit<P, "id"> & {
|
|
13
|
+
id?: string | undefined;
|
|
14
|
+
}): C;
|
|
15
|
+
/** A part's own options as a core signal that follows each render, after it commits. */
|
|
16
|
+
export declare function useOptions<T extends Record<string, unknown>>(options: T): ReadonlySignal<T>;
|
|
17
|
+
export type RenderProp<State = undefined> = ReactElement<Record<string, unknown>> | ((props: Record<string, unknown>, state: State) => ReactElement);
|
|
18
|
+
export interface PartComponentProps<State = undefined> {
|
|
19
|
+
/** Renders the part as another element or component. The part's attributes still win. */
|
|
20
|
+
render?: RenderProp<State> | undefined;
|
|
21
|
+
children?: ReactNode;
|
|
22
|
+
ref?: Ref<HTMLElement> | undefined;
|
|
23
|
+
[attribute: string]: unknown;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Renders a part: its element (or the caller's `render`), with the caller's props merged under
|
|
27
|
+
* the part's, and the part's DOM work attached to the element.
|
|
28
|
+
*/
|
|
29
|
+
export declare function usePart<State = undefined>(part: Part, tag: string, { render, ref, ...caller }: PartComponentProps<State>, state?: State): ReactElement;
|
|
30
|
+
/** Its children in a container at the end of the page, once the page has hydrated. */
|
|
31
|
+
export declare function Portal({ children }: {
|
|
32
|
+
children?: ReactNode;
|
|
33
|
+
}): ReactElement | null;
|
|
34
|
+
//# sourceMappingURL=runtime.d.ts.map
|
package/src/runtime.js
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { mergeProps, sameProps, signal } from "@kirana-ui/core";
|
|
2
|
+
import { cloneElement, createElement, isValidElement, useCallback, useEffect, useId, useLayoutEffect, useRef, useState, useSyncExternalStore } from "react";
|
|
3
|
+
import { createPortal } from "react-dom";
|
|
4
|
+
import { jsx } from "react/jsx-runtime";
|
|
5
|
+
// src/runtime.tsx
|
|
6
|
+
const useIsomorphicLayoutEffect = typeof document === "undefined" ? useEffect : useLayoutEffect;
|
|
7
|
+
/** A core signal's value, re-rendering when it changes. */
|
|
8
|
+
function useSignal(source) {
|
|
9
|
+
const subscribe = useCallback((listener) => source.subscribe(listener), [source]);
|
|
10
|
+
const read = useCallback(() => source.peek(), [source]);
|
|
11
|
+
return useSyncExternalStore(subscribe, read, read);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* A component's controller, made once, following the props of each render. The props reach the
|
|
15
|
+
* core after the render commits (a layout effect), never during it: another part that reads the
|
|
16
|
+
* controller is another component, and React forbids updating it mid-render.
|
|
17
|
+
*/
|
|
18
|
+
function useController(create, props) {
|
|
19
|
+
const generated = useId();
|
|
20
|
+
const next = {
|
|
21
|
+
...props,
|
|
22
|
+
id: props.id ?? generated
|
|
23
|
+
};
|
|
24
|
+
const latest = useRef(next);
|
|
25
|
+
latest.current = next;
|
|
26
|
+
const [state] = useState(() => {
|
|
27
|
+
const stable = {};
|
|
28
|
+
for (const key in next) stable[key] = typeof next[key] === "function" ? (...args) => latest.current[key]?.(...args) : next[key];
|
|
29
|
+
const source = signal(stable, sameProps);
|
|
30
|
+
return {
|
|
31
|
+
source,
|
|
32
|
+
controller: create(source)
|
|
33
|
+
};
|
|
34
|
+
});
|
|
35
|
+
useIsomorphicLayoutEffect(() => {
|
|
36
|
+
const stable = { ...state.source.peek() };
|
|
37
|
+
for (const key in next) if (typeof next[key] !== "function") stable[key] = next[key];
|
|
38
|
+
state.source.set(stable);
|
|
39
|
+
});
|
|
40
|
+
useEffect(() => state.controller.mount(), [state.controller]);
|
|
41
|
+
return state.controller;
|
|
42
|
+
}
|
|
43
|
+
/** A part's own options as a core signal that follows each render, after it commits. */
|
|
44
|
+
function useOptions(options) {
|
|
45
|
+
const [source] = useState(() => signal(options, sameProps));
|
|
46
|
+
useIsomorphicLayoutEffect(() => source.set(options));
|
|
47
|
+
return source;
|
|
48
|
+
}
|
|
49
|
+
const BOOLEAN = /* @__PURE__ */ new Set([
|
|
50
|
+
"hidden",
|
|
51
|
+
"disabled",
|
|
52
|
+
"inert",
|
|
53
|
+
"required",
|
|
54
|
+
"readonly",
|
|
55
|
+
"multiple",
|
|
56
|
+
"checked",
|
|
57
|
+
"novalidate"
|
|
58
|
+
]);
|
|
59
|
+
const RENAMED = {
|
|
60
|
+
tabindex: "tabIndex",
|
|
61
|
+
for: "htmlFor",
|
|
62
|
+
readonly: "readOnly",
|
|
63
|
+
novalidate: "noValidate"
|
|
64
|
+
};
|
|
65
|
+
/** A style as React takes it: `inset-inline-start` is `insetInlineStart`, `--x` stays as it is. */
|
|
66
|
+
function reactStyle(style) {
|
|
67
|
+
const out = {};
|
|
68
|
+
for (const name in style) {
|
|
69
|
+
const key = name.startsWith("--") ? name : name.replace(/-([a-z])/g, (_match, letter) => letter.toUpperCase());
|
|
70
|
+
out[key] = style[name];
|
|
71
|
+
}
|
|
72
|
+
return out;
|
|
73
|
+
}
|
|
74
|
+
function toReact(props) {
|
|
75
|
+
const out = {};
|
|
76
|
+
for (const key in props) {
|
|
77
|
+
const value = props[key];
|
|
78
|
+
if (BOOLEAN.has(key)) out[RENAMED[key] ?? key] = value === "" || value === true;
|
|
79
|
+
else if (key === "style" && value) out.style = reactStyle(value);
|
|
80
|
+
else if (value !== void 0) out[RENAMED[key] ?? key] = value;
|
|
81
|
+
}
|
|
82
|
+
return out;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Renders a part: its element (or the caller's `render`), with the caller's props merged under
|
|
86
|
+
* the part's, and the part's DOM work attached to the element.
|
|
87
|
+
*/
|
|
88
|
+
function usePart(part, tag, { render, ref, ...caller }, state) {
|
|
89
|
+
const own = useSignal(part.props);
|
|
90
|
+
const detach = useRef(void 0);
|
|
91
|
+
const attach = useCallback((element) => {
|
|
92
|
+
detach.current?.();
|
|
93
|
+
detach.current = element ? part.attach(element) : void 0;
|
|
94
|
+
if (typeof ref === "function") ref(element);
|
|
95
|
+
else if (ref) ref.current = element;
|
|
96
|
+
}, [part, ref]);
|
|
97
|
+
const props = {
|
|
98
|
+
...toReact(mergeProps(caller, own)),
|
|
99
|
+
ref: attach
|
|
100
|
+
};
|
|
101
|
+
if (typeof render === "function") return render(props, state);
|
|
102
|
+
if (isValidElement(render)) return cloneElement(render, {
|
|
103
|
+
...toReact(mergeProps(render.props, props)),
|
|
104
|
+
ref: attach
|
|
105
|
+
});
|
|
106
|
+
return createElement(tag, props);
|
|
107
|
+
}
|
|
108
|
+
/** Its children in a container at the end of the page, once the page has hydrated. */
|
|
109
|
+
function Portal({ children }) {
|
|
110
|
+
const [ready, setReady] = useState(false);
|
|
111
|
+
useIsomorphicLayoutEffect(() => setReady(true), []);
|
|
112
|
+
if (!ready) return null;
|
|
113
|
+
return createPortal(/* @__PURE__ */ jsx("div", {
|
|
114
|
+
"data-kirana-portal": "",
|
|
115
|
+
children
|
|
116
|
+
}), document.body);
|
|
117
|
+
}
|
|
118
|
+
export { Portal, useController, useOptions, usePart, useSignal };
|
|
119
|
+
|
|
120
|
+
//# sourceMappingURL=runtime.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime.js","names":[],"sources":["../../../../packages/react/src/runtime.tsx"],"sourcesContent":["import { mergeProps, sameProps, signal } from \"@kirana-ui/core\"\nimport type { Part, PartProps, ReadonlySignal, Signal } from \"@kirana-ui/core\"\nimport {\n cloneElement,\n createElement,\n isValidElement,\n useCallback,\n useEffect,\n useId,\n useLayoutEffect,\n useRef,\n useState,\n useSyncExternalStore,\n} from \"react\"\nimport type { ReactElement, ReactNode, Ref } from \"react\"\nimport { createPortal } from \"react-dom\"\n\nconst useIsomorphicLayoutEffect = typeof document === \"undefined\" ? useEffect : useLayoutEffect\n\n/** A core signal's value, re-rendering when it changes. */\nexport function useSignal<T>(source: ReadonlySignal<T>): T {\n const subscribe = useCallback((listener: () => void) => source.subscribe(listener), [source])\n const read = useCallback(() => source.peek(), [source])\n return useSyncExternalStore(subscribe, read, read)\n}\n\n/**\n * A component's controller, made once, following the props of each render. The props reach the\n * core after the render commits (a layout effect), never during it: another part that reads the\n * controller is another component, and React forbids updating it mid-render.\n */\nexport function useController<P extends object, C extends { mount(): () => void }>(\n create: (props: ReadonlySignal<P>) => C,\n props: Omit<P, \"id\"> & { id?: string | undefined }\n): C {\n const generated = useId()\n const next = { ...props, id: props.id ?? generated } as P\n const latest = useRef(next)\n latest.current = next\n const [state] = useState(() => {\n // The callbacks the core holds always call the latest render's.\n const stable = {} as Record<string, unknown>\n for (const key in next) {\n stable[key] =\n typeof next[key] === \"function\"\n ? (...args: unknown[]) =>\n (latest.current[key] as ((...args: unknown[]) => unknown) | undefined)?.(...args)\n : next[key]\n }\n const source: Signal<P> = signal(stable as P, sameProps as (a: P, b: P) => boolean)\n return { source, controller: create(source) }\n })\n useIsomorphicLayoutEffect(() => {\n const stable = { ...state.source.peek() } as Record<string, unknown>\n for (const key in next) {\n if (typeof next[key] !== \"function\") stable[key] = next[key]\n }\n state.source.set(stable as P)\n })\n useEffect(() => state.controller.mount(), [state.controller])\n return state.controller\n}\n\n/** A part's own options as a core signal that follows each render, after it commits. */\nexport function useOptions<T extends Record<string, unknown>>(options: T): ReadonlySignal<T> {\n const [source] = useState(() => signal(options, sameProps as (a: T, b: T) => boolean))\n useIsomorphicLayoutEffect(() => source.set(options))\n return source\n}\n\n// HTML's boolean attributes, which React takes as booleans: a part's `\"\"` means \"present\".\nconst BOOLEAN = new Set([\n \"hidden\",\n \"disabled\",\n \"inert\",\n \"required\",\n \"readonly\",\n \"multiple\",\n \"checked\",\n \"novalidate\",\n])\nconst RENAMED: Record<string, string> = {\n tabindex: \"tabIndex\",\n for: \"htmlFor\",\n readonly: \"readOnly\",\n novalidate: \"noValidate\",\n}\n\n/** A style as React takes it: `inset-inline-start` is `insetInlineStart`, `--x` stays as it is. */\nfunction reactStyle(style: Record<string, string>): Record<string, string> {\n const out: Record<string, string> = {}\n for (const name in style) {\n const key = name.startsWith(\"--\")\n ? name\n : name.replace(/-([a-z])/g, (_match, letter: string) => letter.toUpperCase())\n out[key] = style[name]!\n }\n return out\n}\n\nfunction toReact(props: PartProps): Record<string, unknown> {\n const out: Record<string, unknown> = {}\n for (const key in props) {\n const value = props[key]\n // Always a boolean, never left out: an input whose `checked` comes and goes is, to React,\n // one that changes from controlled to not.\n if (BOOLEAN.has(key)) out[RENAMED[key] ?? key] = value === \"\" || value === true\n else if (key === \"style\" && value) out.style = reactStyle(value as Record<string, string>)\n else if (value !== undefined) out[RENAMED[key] ?? key] = value\n }\n return out\n}\n\nexport type RenderProp<State = undefined> =\n | ReactElement<Record<string, unknown>>\n | ((props: Record<string, unknown>, state: State) => ReactElement)\n\nexport interface PartComponentProps<State = undefined> {\n /** Renders the part as another element or component. The part's attributes still win. */\n render?: RenderProp<State> | undefined\n children?: ReactNode\n ref?: Ref<HTMLElement> | undefined\n [attribute: string]: unknown\n}\n\n/**\n * Renders a part: its element (or the caller's `render`), with the caller's props merged under\n * the part's, and the part's DOM work attached to the element.\n */\nexport function usePart<State = undefined>(\n part: Part,\n tag: string,\n { render, ref, ...caller }: PartComponentProps<State>,\n state?: State\n): ReactElement {\n const own = useSignal(part.props)\n const detach = useRef<(() => void) | undefined>(undefined)\n const attach = useCallback(\n (element: HTMLElement | null) => {\n detach.current?.()\n detach.current = element ? part.attach(element) : undefined\n if (typeof ref === \"function\") ref(element)\n else if (ref) (ref as { current: HTMLElement | null }).current = element\n },\n [part, ref]\n )\n const props = { ...toReact(mergeProps(caller, own)), ref: attach }\n if (typeof render === \"function\") return render(props, state as State)\n if (isValidElement(render)) {\n return cloneElement(render, { ...toReact(mergeProps(render.props, props)), ref: attach })\n }\n return createElement(tag, props)\n}\n\n/** Its children in a container at the end of the page, once the page has hydrated. */\nexport function Portal({ children }: { children?: ReactNode }): ReactElement | null {\n const [ready, setReady] = useState(false)\n useIsomorphicLayoutEffect(() => setReady(true), [])\n if (!ready) return null\n return createPortal(<div data-kirana-portal=\"\">{children}</div>, document.body)\n}\n"],"mappings":";;;;;AAiBA,MAAM,4BAA4B,OAAO,aAAa,cAAc,YAAY;;AAGhF,SAAgB,UAAa,QAA8B;CACzD,MAAM,YAAY,aAAa,aAAyB,OAAO,UAAU,QAAQ,GAAG,CAAC,MAAM,CAAC;CAC5F,MAAM,OAAO,kBAAkB,OAAO,KAAK,GAAG,CAAC,MAAM,CAAC;CACtD,OAAO,qBAAqB,WAAW,MAAM,IAAI;AACnD;;;;;;AAOA,SAAgB,cACd,QACA,OACG;CACH,MAAM,YAAY,MAAM;CACxB,MAAM,OAAO;EAAE,GAAG;EAAO,IAAI,MAAM,MAAM;CAAU;CACnD,MAAM,SAAS,OAAO,IAAI;CAC1B,OAAO,UAAU;CACjB,MAAM,CAAC,SAAS,eAAe;EAE7B,MAAM,SAAS,CAAC;EAChB,KAAK,MAAM,OAAO,MAChB,OAAO,OACL,OAAO,KAAK,SAAS,cAChB,GAAG,SACD,OAAO,QAAQ,IAAI,GAAqD,GAAG,IAAI,IAClF,KAAK;EAEb,MAAM,SAAoB,OAAO,QAAa,SAAoC;EAClF,OAAO;GAAE;GAAQ,YAAY,OAAO,MAAM;EAAE;CAC9C,CAAC;CACD,gCAAgC;EAC9B,MAAM,SAAS,EAAE,GAAG,MAAM,OAAO,KAAK,EAAE;EACxC,KAAK,MAAM,OA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B,OAAO,UAAU,UAAU,KAAK,OAAO,OAAO,IAAI,KAAA;EAClD,IAAI,OAAO,QAAQ,YAAY,IAAI,OAAO;OACrC,IAAI,KAAK,IAAyC,UAAU;CACnE,GACA,CAAC,MAAM,GAAG,CACZ;CACA,MAAM,QAAQ;EAAE,GAAG,QAAQ,WAAW,QAAQ,GAAG,CAAC;EAAG,KAAK;CAAO;CACjE,IAAI,OAAO,WAAW,YAAY,OAAO,OAAO,OAAO,KAAc;CACrE,IAAI,eAAe,MAAM,GACvB,OAAO,aAAa,QAAQ;EAAE,GAAG,QAAQ,WAAW,OAAO,OAAO,KAAK,CAAC;EAAG,KAAK;CAAO,CAAC;CAE1F,OAAO,cAAc,KAAK,KAAK;AACjC;;AAGA,SAAgB,OAAO,EAAE,YAA2D;CAClF,MAAM,CAAC,OAAO,YAAY,SAAS,KAAK;CACxC,gCAAgC,SAAS,IAAI,GAAG,CAAC,CAAC;CAClD,IAAI,CAAC,OAAO,OAAO;CACnB,OAAO,aAAa,oBAAC,OAAD;EAAK,sBAAmB;EAAI;CAAc,CAAA,GAAG,SAAS,IAAI;AAChF"}
|
package/src/select.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { PartComponentProps } from "./runtime.js";
|
|
2
|
+
import { PositionOptions } from "@kirana-ui/core";
|
|
3
|
+
import { ReactElement, ReactNode } from "react";
|
|
4
|
+
import { SelectProps } from "@kirana-ui/core/select";
|
|
5
|
+
export type RootProps<Value> = Omit<SelectProps<Value>, "id"> & {
|
|
6
|
+
id?: string;
|
|
7
|
+
children?: ReactNode;
|
|
8
|
+
};
|
|
9
|
+
declare function Root<Value>({ children, ...props }: RootProps<Value>): ReactElement;
|
|
10
|
+
declare const Trigger: (props: PartComponentProps) => ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
|
|
11
|
+
export interface ValueProps extends PartComponentProps {
|
|
12
|
+
/** Shown while nothing is chosen. */
|
|
13
|
+
placeholder?: ReactNode;
|
|
14
|
+
}
|
|
15
|
+
declare function Value({ placeholder, children, ...props }: ValueProps): ReactElement;
|
|
16
|
+
declare function Portal({ children }: {
|
|
17
|
+
children?: ReactNode;
|
|
18
|
+
}): ReactElement | null;
|
|
19
|
+
export type PositionerProps = PartComponentProps & Partial<PositionOptions>;
|
|
20
|
+
declare function Positioner({ side, align, sideOffset, alignOffset, collisionPadding, ...props }: PositionerProps): ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
|
|
21
|
+
declare const Popup: (props: PartComponentProps) => ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
|
|
22
|
+
export interface ItemProps extends PartComponentProps {
|
|
23
|
+
value: unknown;
|
|
24
|
+
disabled?: boolean;
|
|
25
|
+
/** What typeahead matches and the trigger shows; the item's text when left out. */
|
|
26
|
+
label?: string;
|
|
27
|
+
}
|
|
28
|
+
declare function Item({ value, disabled, label, ...props }: ItemProps): ReactElement;
|
|
29
|
+
declare function ItemText(props: PartComponentProps): ReactElement;
|
|
30
|
+
export interface ItemIndicatorProps extends PartComponentProps {
|
|
31
|
+
keepMounted?: boolean;
|
|
32
|
+
}
|
|
33
|
+
declare function ItemIndicator({ keepMounted, render: _render, ref: _ref, ...rest }: ItemIndicatorProps): import("react").JSX.Element | null;
|
|
34
|
+
export declare const Select: {
|
|
35
|
+
Root: typeof Root;
|
|
36
|
+
Trigger: typeof Trigger;
|
|
37
|
+
Value: typeof Value;
|
|
38
|
+
Portal: typeof Portal;
|
|
39
|
+
Positioner: typeof Positioner;
|
|
40
|
+
Popup: typeof Popup;
|
|
41
|
+
Item: typeof Item;
|
|
42
|
+
ItemText: typeof ItemText;
|
|
43
|
+
ItemIndicator: typeof ItemIndicator;
|
|
44
|
+
};
|
|
45
|
+
export {};
|
|
46
|
+
//# sourceMappingURL=select.d.ts.map
|
package/src/select.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { FieldContext } from "./field-context.js";
|
|
2
|
+
import { Portal as Portal$1, useController, useOptions, usePart, useSignal } from "./runtime.js";
|
|
3
|
+
import { defined, within } from "@kirana-ui/core";
|
|
4
|
+
import { createContext, useContext, useMemo } from "react";
|
|
5
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
6
|
+
import { createSelect } from "@kirana-ui/core/select";
|
|
7
|
+
// src/select.tsx
|
|
8
|
+
const Context = createContext(null);
|
|
9
|
+
const ItemContext = createContext(void 0);
|
|
10
|
+
function useSelect(part) {
|
|
11
|
+
const controller = useContext(Context);
|
|
12
|
+
if (!controller) throw new Error(`<Select.${part}> must be inside <Select.Root>`);
|
|
13
|
+
return controller;
|
|
14
|
+
}
|
|
15
|
+
function Root({ children, ...props }) {
|
|
16
|
+
const field = useContext(FieldContext) ?? void 0;
|
|
17
|
+
const controller = useController(within(createSelect, { field }), props);
|
|
18
|
+
const named = useSignal(controller.named);
|
|
19
|
+
return /* @__PURE__ */ jsxs(Context.Provider, {
|
|
20
|
+
value: controller,
|
|
21
|
+
children: [children, named ? /* @__PURE__ */ jsx(HiddenInput, {}) : null]
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
const HiddenInput = () => usePart(useSelect("Root").input, "input", {});
|
|
25
|
+
const Trigger = (props) => usePart(useSelect("Trigger").trigger, "button", props);
|
|
26
|
+
function Value({ placeholder, children, ...props }) {
|
|
27
|
+
const controller = useSelect("Value");
|
|
28
|
+
const label = useSignal(controller.label);
|
|
29
|
+
return usePart(controller.valuePart, "span", {
|
|
30
|
+
...props,
|
|
31
|
+
children: children ?? label ?? placeholder
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
function Portal({ children }) {
|
|
35
|
+
return useSignal(useSelect("Portal").mounted) ? /* @__PURE__ */ jsx(Portal$1, { children }) : null;
|
|
36
|
+
}
|
|
37
|
+
function Positioner({ side, align, sideOffset, alignOffset, collisionPadding, ...props }) {
|
|
38
|
+
const controller = useSelect("Positioner");
|
|
39
|
+
controller.positioning.set(defined({
|
|
40
|
+
side,
|
|
41
|
+
align,
|
|
42
|
+
sideOffset,
|
|
43
|
+
alignOffset,
|
|
44
|
+
collisionPadding
|
|
45
|
+
}));
|
|
46
|
+
return usePart(controller.positioner, "div", props);
|
|
47
|
+
}
|
|
48
|
+
const Popup = (props) => usePart(useSelect("Popup").popup, "div", props);
|
|
49
|
+
function Item({ value, disabled, label, ...props }) {
|
|
50
|
+
const controller = useSelect("Item");
|
|
51
|
+
const options = useOptions({
|
|
52
|
+
disabled,
|
|
53
|
+
label
|
|
54
|
+
});
|
|
55
|
+
const part = useMemo(() => controller.item(value, () => defined(options.get())), [
|
|
56
|
+
controller,
|
|
57
|
+
value,
|
|
58
|
+
options
|
|
59
|
+
]);
|
|
60
|
+
return /* @__PURE__ */ jsx(ItemContext.Provider, {
|
|
61
|
+
value,
|
|
62
|
+
children: usePart(part, "div", props)
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
function ItemText(props) {
|
|
66
|
+
const controller = useSelect("ItemText");
|
|
67
|
+
const value = useContext(ItemContext);
|
|
68
|
+
const part = useMemo(() => controller.itemText(value), [controller, value]);
|
|
69
|
+
return usePart(part, "span", props);
|
|
70
|
+
}
|
|
71
|
+
function ItemIndicator({ keepMounted, render: _render, ref: _ref, ...rest }) {
|
|
72
|
+
if (!useSignal(useSelect("ItemIndicator").selected(useContext(ItemContext))) && !keepMounted) return null;
|
|
73
|
+
return /* @__PURE__ */ jsx("span", {
|
|
74
|
+
"aria-hidden": "true",
|
|
75
|
+
...rest
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
const Select = {
|
|
79
|
+
Root,
|
|
80
|
+
Trigger,
|
|
81
|
+
Value,
|
|
82
|
+
Portal,
|
|
83
|
+
Positioner,
|
|
84
|
+
Popup,
|
|
85
|
+
Item,
|
|
86
|
+
ItemText,
|
|
87
|
+
ItemIndicator
|
|
88
|
+
};
|
|
89
|
+
export { Select };
|
|
90
|
+
|
|
91
|
+
//# sourceMappingURL=select.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"select.js","names":["BodyPortal"],"sources":["../../../../packages/react/src/select.tsx"],"sourcesContent":["import { defined, within } from \"@kirana-ui/core\"\nimport type { PositionOptions } from \"@kirana-ui/core\"\nimport { createSelect } from \"@kirana-ui/core/select\"\nimport type { Select as Controller, SelectProps } from \"@kirana-ui/core/select\"\nimport { createContext, useContext, useMemo } from \"react\"\nimport type { ReactElement, ReactNode } from \"react\"\n\nimport { FieldContext } from \"./field-context.ts\"\nimport { Portal as BodyPortal, useController, useOptions, usePart, useSignal } from \"./runtime.tsx\"\nimport type { PartComponentProps } from \"./runtime.tsx\"\n\nconst Context = createContext<Controller | null>(null)\nconst ItemContext = createContext<unknown>(undefined)\n\nfunction useSelect(part: string): Controller {\n const controller = useContext(Context)\n if (!controller) throw new Error(`<Select.${part}> must be inside <Select.Root>`)\n return controller\n}\n\nexport type RootProps<Value> = Omit<SelectProps<Value>, \"id\"> & {\n id?: string\n children?: ReactNode\n}\n\nfunction Root<Value>({ children, ...props }: RootProps<Value>): ReactElement {\n const field = useContext(FieldContext) ?? undefined\n const controller = useController(\n within(createSelect<Value>, { field }),\n props\n ) as unknown as Controller\n const named = useSignal(controller.named)\n return (\n <Context.Provider value={controller}>\n {children}\n {named ? <HiddenInput /> : null}\n </Context.Provider>\n )\n}\n\nconst HiddenInput = () => usePart(useSelect(\"Root\").input, \"input\", {})\n\nconst Trigger = (props: PartComponentProps) =>\n usePart(useSelect(\"Trigger\").trigger, \"button\", props)\n\nexport interface ValueProps extends PartComponentProps {\n /** Shown while nothing is chosen. */\n placeholder?: ReactNode\n}\n\nfunction Value({ placeholder, children, ...props }: ValueProps): ReactElement {\n const controller = useSelect(\"Value\")\n const label = useSignal(controller.label)\n return usePart(controller.valuePart, \"span\", {\n ...props,\n children: children ?? label ?? placeholder,\n })\n}\n\nfunction Portal({ children }: { children?: ReactNode }): ReactElement | null {\n const mounted = useSignal(useSelect(\"Portal\").mounted)\n return mounted ? <BodyPortal>{children}</BodyPortal> : null\n}\n\nexport type PositionerProps = PartComponentProps & Partial<PositionOptions>\n\nfunction Positioner({\n side,\n align,\n sideOffset,\n alignOffset,\n collisionPadding,\n ...props\n}: PositionerProps) {\n const controller = useSelect(\"Positioner\")\n controller.positioning.set(defined({ side, align, sideOffset, alignOffset, collisionPadding }))\n return usePart(controller.positioner, \"div\", props)\n}\n\nconst Popup = (props: PartComponentProps) => usePart(useSelect(\"Popup\").popup, \"div\", props)\n\nexport interface ItemProps extends PartComponentProps {\n value: unknown\n disabled?: boolean\n /** What typeahead matches and the trigger shows; the item's text when left out. */\n label?: string\n}\n\nfunction Item({ value, disabled, label, ...props }: ItemProps): ReactElement {\n const controller = useSelect(\"Item\")\n const options = useOptions({ disabled, label })\n const part = useMemo(\n () => controller.item(value, () => defined(options.get())),\n [controller, value, options]\n )\n return <ItemContext.Provider value={value}>{usePart(part, \"div\", props)}</ItemContext.Provider>\n}\n\nfunction ItemText(props: PartComponentProps): ReactElement {\n const controller = useSelect(\"ItemText\")\n const value = useContext(ItemContext)\n const part = useMemo(() => controller.itemText(value), [controller, value])\n return usePart(part, \"span\", props)\n}\n\nexport interface ItemIndicatorProps extends PartComponentProps {\n keepMounted?: boolean\n}\n\nfunction ItemIndicator({ keepMounted, render: _render, ref: _ref, ...rest }: ItemIndicatorProps) {\n const selected = useSignal(useSelect(\"ItemIndicator\").selected(useContext(ItemContext)))\n if (!selected && !keepMounted) return null\n return <span aria-hidden=\"true\" {...(rest as object)} />\n}\n\nexport const Select = {\n Root,\n Trigger,\n Value,\n Portal,\n Positioner,\n Popup,\n Item,\n ItemText,\n ItemIndicator,\n}\n"],"mappings":";;;;;;;AAWA,MAAM,UAAU,cAAiC,IAAI;AACrD,MAAM,cAAc,cAAuB,KAAA,CAAS;AAEpD,SAAS,UAAU,MAA0B;CAC3C,MAAM,aAAa,WAAW,OAAO;CACrC,IAAI,CAAC,YAAY,MAAM,IAAI,MAAM,WAAW,KAAK,+BAA+B;CAChF,OAAO;AACT;AAOA,SAAS,KAAY,EAA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cACL,WAAW,KAAK,aAAa,QAAQ,QAAQ,IAAI,CAAC,CAAC,GACzD;EAAC;EAAY;EAAO;CAAO,CAC7B;CACA,OAAO,oBAAC,YAAY,UAAb;EAA6B;EAAQ,UAAA,QAAQ,MAAM,OAAO,KAAK;CAAwB,CAAA;AAChG;AAEA,SAAS,SAAS,OAAyC;CACzD,MAAM,aAAa,UAAU,UAAU;CACvC,MAAM,QAAQ,WAAW,WAAW;CACpC,MAAM,OAAO,cAAc,WAAW,SAAS,KAAK,GAAG,CAAC,YAAY,KAAK,CAAC;CAC1E,OAAO,QAAQ,MAAM,QAAQ,KAAK;AACpC;AAMA,SAAS,cAAc,EAAE,aAAa,QAAQ,SAAS,KAAK,MAAM,GAAG,QAA4B;CAE/F,IAAI,CADa,UAAU,UAAU,eAAe,CAAC,CAAC,SAAS,WAAW,WAAW,CAAC,CAC1E,KAAK,CAAC,aAAa,OAAO;CACtC,OAAO,oBAAC,QAAD;EAAM,eAAY;EAAO,GAAK;CAAkB,CAAA;AACzD;AAEA,MAAa,SAAS;CACpB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { PartComponentProps } from "./runtime.js";
|
|
2
|
+
import { ReactElement } from "react";
|
|
3
|
+
import { SeparatorProps as SeparatorProps$1 } from "@kirana-ui/core/separator";
|
|
4
|
+
export type SeparatorProps = SeparatorProps$1 & PartComponentProps;
|
|
5
|
+
export declare function Separator({ orientation, ...rest }: SeparatorProps): ReactElement;
|
|
6
|
+
//# sourceMappingURL=separator.d.ts.map
|
package/src/separator.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { useController, usePart } from "./runtime.js";
|
|
2
|
+
import { createSeparator } from "@kirana-ui/core/separator";
|
|
3
|
+
// src/separator.tsx
|
|
4
|
+
function Separator({ orientation, ...rest }) {
|
|
5
|
+
const controller = useController(createSeparator, { orientation });
|
|
6
|
+
return usePart(controller.root, "div", rest);
|
|
7
|
+
}
|
|
8
|
+
export { Separator };
|
|
9
|
+
|
|
10
|
+
//# sourceMappingURL=separator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"separator.js","names":[],"sources":["../../../../packages/react/src/separator.tsx"],"sourcesContent":["import { createSeparator } from \"@kirana-ui/core/separator\"\nimport type { SeparatorProps as CoreProps } from \"@kirana-ui/core/separator\"\nimport type { ReactElement } from \"react\"\n\nimport { useController, usePart } from \"./runtime.tsx\"\nimport type { PartComponentProps } from \"./runtime.tsx\"\n\nexport type SeparatorProps = CoreProps & PartComponentProps\n\nexport function Separator({ orientation, ...rest }: SeparatorProps): ReactElement {\n const controller = useController(createSeparator, { orientation })\n return usePart(controller.root, \"div\", rest)\n}\n"],"mappings":";;;AASA,SAAgB,UAAU,EAAE,aAAa,GAAG,QAAsC;CAChF,MAAM,aAAa,cAAc,iBAAiB,EAAE,YAAY,CAAC;CACjE,OAAO,QAAQ,WAAW,MAAM,OAAO,IAAI;AAC7C"}
|
package/src/slider.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { PartComponentProps } from "./runtime.js";
|
|
2
|
+
import { ReactElement, ReactNode } from "react";
|
|
3
|
+
import { SliderProps } from "@kirana-ui/core/slider";
|
|
4
|
+
export type RootProps = Omit<SliderProps, "id"> & {
|
|
5
|
+
id?: string;
|
|
6
|
+
} & PartComponentProps;
|
|
7
|
+
declare function Root({ value, defaultValue, onValueChange, onValueCommitted, min, max, step, largeStep, minStepsBetweenValues, orientation, disabled, name, format, locale, getAriaValueText, id, ...rest }: RootProps): ReactElement;
|
|
8
|
+
declare const Control: (props: PartComponentProps) => ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
|
|
9
|
+
declare const Track: (props: PartComponentProps) => ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
|
|
10
|
+
declare const Indicator: (props: PartComponentProps) => ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
|
|
11
|
+
export interface ThumbProps extends PartComponentProps {
|
|
12
|
+
/** Which value of a range the thumb is, from 0. */
|
|
13
|
+
index?: number;
|
|
14
|
+
}
|
|
15
|
+
declare function Thumb({ index, children, ...rest }: ThumbProps): ReactElement;
|
|
16
|
+
/** The written value, or what `children` makes of it and of the values. */
|
|
17
|
+
declare function Value({ children, ...props }: Omit<PartComponentProps, "children"> & {
|
|
18
|
+
children?: (text: string, values: readonly number[]) => ReactNode;
|
|
19
|
+
}): ReactElement;
|
|
20
|
+
export declare const Slider: {
|
|
21
|
+
Root: typeof Root;
|
|
22
|
+
Value: typeof Value;
|
|
23
|
+
Control: typeof Control;
|
|
24
|
+
Track: typeof Track;
|
|
25
|
+
Indicator: typeof Indicator;
|
|
26
|
+
Thumb: typeof Thumb;
|
|
27
|
+
};
|
|
28
|
+
export {};
|
|
29
|
+
//# sourceMappingURL=slider.d.ts.map
|
package/src/slider.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { DirectionContext } from "./direction-context.js";
|
|
2
|
+
import { FieldContext } from "./field-context.js";
|
|
3
|
+
import { useController, usePart, useSignal } from "./runtime.js";
|
|
4
|
+
import { within } from "@kirana-ui/core";
|
|
5
|
+
import { createContext, useContext } from "react";
|
|
6
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
7
|
+
import { createSlider } from "@kirana-ui/core/slider";
|
|
8
|
+
// src/slider.tsx
|
|
9
|
+
const Context = createContext(null);
|
|
10
|
+
function useSlider(part) {
|
|
11
|
+
const controller = useContext(Context);
|
|
12
|
+
if (!controller) throw new Error(`<Slider.${part}> must be inside <Slider.Root>`);
|
|
13
|
+
return controller;
|
|
14
|
+
}
|
|
15
|
+
function Root({ value, defaultValue, onValueChange, onValueCommitted, min, max, step, largeStep, minStepsBetweenValues, orientation, disabled, name, format, locale, getAriaValueText, id, ...rest }) {
|
|
16
|
+
const field = useContext(FieldContext) ?? void 0;
|
|
17
|
+
const direction = useContext(DirectionContext) ?? void 0;
|
|
18
|
+
const create = within(createSlider, {
|
|
19
|
+
field,
|
|
20
|
+
direction
|
|
21
|
+
});
|
|
22
|
+
const controller = useController(create, {
|
|
23
|
+
value,
|
|
24
|
+
defaultValue,
|
|
25
|
+
onValueChange,
|
|
26
|
+
onValueCommitted,
|
|
27
|
+
min,
|
|
28
|
+
max,
|
|
29
|
+
step,
|
|
30
|
+
largeStep,
|
|
31
|
+
minStepsBetweenValues,
|
|
32
|
+
orientation,
|
|
33
|
+
disabled,
|
|
34
|
+
name,
|
|
35
|
+
format,
|
|
36
|
+
locale,
|
|
37
|
+
getAriaValueText,
|
|
38
|
+
id
|
|
39
|
+
});
|
|
40
|
+
return /* @__PURE__ */ jsx(Context.Provider, {
|
|
41
|
+
value: controller,
|
|
42
|
+
children: /* @__PURE__ */ jsx(RootElement, { ...rest })
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
const RootElement = (props) => usePart(useSlider("Root").root, "div", props);
|
|
46
|
+
const Control = (props) => usePart(useSlider("Control").control, "div", props);
|
|
47
|
+
const Track = (props) => usePart(useSlider("Track").track, "div", props);
|
|
48
|
+
const Indicator = (props) => usePart(useSlider("Indicator").indicator, "div", props);
|
|
49
|
+
const PartElement = ({ part, tag, ...props }) => usePart(part, tag, props);
|
|
50
|
+
function Thumb({ index = 0, children, ...rest }) {
|
|
51
|
+
const controller = useSlider("Thumb");
|
|
52
|
+
return /* @__PURE__ */ jsxs(PartElement, {
|
|
53
|
+
part: controller.thumb(index),
|
|
54
|
+
tag: "div",
|
|
55
|
+
...rest,
|
|
56
|
+
children: [children, /* @__PURE__ */ jsx(PartElement, {
|
|
57
|
+
part: controller.thumbInput(index),
|
|
58
|
+
tag: "input"
|
|
59
|
+
})]
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
/** The written value, or what `children` makes of it and of the values. */
|
|
63
|
+
function Value({ children, ...props }) {
|
|
64
|
+
const controller = useSlider("Value");
|
|
65
|
+
const text = useSignal(controller.text);
|
|
66
|
+
const values = useSignal(controller.values);
|
|
67
|
+
return usePart(controller.value, "output", {
|
|
68
|
+
...props,
|
|
69
|
+
children: children ? children(text, values) : text
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
const Slider = {
|
|
73
|
+
Root,
|
|
74
|
+
Value,
|
|
75
|
+
Control,
|
|
76
|
+
Track,
|
|
77
|
+
Indicator,
|
|
78
|
+
Thumb
|
|
79
|
+
};
|
|
80
|
+
export { Slider };
|
|
81
|
+
|
|
82
|
+
//# sourceMappingURL=slider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"slider.js","names":[],"sources":["../../../../packages/react/src/slider.tsx"],"sourcesContent":["import { within } from \"@kirana-ui/core\"\nimport type { Part } from \"@kirana-ui/core\"\nimport { createSlider } from \"@kirana-ui/core/slider\"\nimport type { Slider as Controller, SliderProps } from \"@kirana-ui/core/slider\"\nimport { createContext, useContext } from \"react\"\nimport type { ReactElement, ReactNode } from \"react\"\n\nimport { DirectionContext } from \"./direction-context.ts\"\nimport { FieldContext } from \"./field-context.ts\"\nimport { useController, usePart, useSignal } from \"./runtime.tsx\"\nimport type { PartComponentProps } from \"./runtime.tsx\"\n\nconst Context = createContext<Controller | null>(null)\n\nfunction useSlider(part: string): Controller {\n const controller = useContext(Context)\n if (!controller) throw new Error(`<Slider.${part}> must be inside <Slider.Root>`)\n return controller\n}\n\nexport type RootProps = Omit<SliderProps, \"id\"> & { id?: string } & PartComponentProps\n\nfunction Root({\n value,\n defaultValue,\n onValueChange,\n onValueCommitted,\n min,\n max,\n step,\n largeStep,\n minStepsBetweenValues,\n orientation,\n disabled,\n name,\n format,\n locale,\n getAriaValueText,\n id,\n ...rest\n}: RootProps): ReactElement {\n const field = useContext(FieldContext) ?? undefined\n const direction = useContext(DirectionContext) ?? undefined\n const create = within(createSlider, { field, direction })\n const controller = useController(create, {\n value,\n defaultValue,\n onValueChange,\n onValueCommitted,\n min,\n max,\n step,\n largeStep,\n minStepsBetweenValues,\n orientation,\n disabled,\n name,\n format,\n locale,\n getAriaValueText,\n id,\n })\n return (\n <Context.Provider value={controller}>\n <RootElement {...rest} />\n </Context.Provider>\n )\n}\n\nconst RootElement = (props: PartComponentProps) => usePart(useSlider(\"Root\").root, \"div\", props)\nconst Control = (props: PartComponentProps) => usePart(useSlider(\"Control\").control, \"div\", props)\nconst Track = (props: PartComponentProps) => usePart(useSlider(\"Track\").track, \"div\", props)\nconst Indicator = (props: PartComponentProps) =>\n usePart(useSlider(\"Indicator\").indicator, \"div\", props)\n\nconst PartElement = ({ part, tag, ...props }: PartComponentProps & { part: Part; tag: string }) =>\n usePart(part, tag, props)\n\nexport interface ThumbProps extends PartComponentProps {\n /** Which value of a range the thumb is, from 0. */\n index?: number\n}\n\nfunction Thumb({ index = 0, children, ...rest }: ThumbProps): ReactElement {\n const controller = useSlider(\"Thumb\")\n return (\n <PartElement part={controller.thumb(index)} tag=\"div\" {...rest}>\n {children}\n <PartElement part={controller.thumbInput(index)} tag=\"input\" />\n </PartElement>\n )\n}\n\n/** The written value, or what `children` makes of it and of the values. */\nfunction Value({\n children,\n ...props\n}: Omit<PartComponentProps, \"children\"> & {\n children?: (text: string, values: readonly number[]) => ReactNode\n}): ReactElement {\n const controller = useSlider(\"Value\")\n const text = useSignal(controller.text)\n const values = useSignal(controller.values)\n return usePart(controller.value, \"output\", {\n ...props,\n children: children ? children(text, values) : text,\n })\n}\n\nexport const Slider = { Root, Value, Control, Track, Indicator, Thumb }\n"],"mappings":";;;;;;;;AAYA,MAAM,UAAU,cAAiC,IAAI;AAErD,SAAS,UAAU,MAA0B;CAC3C,MAAM,aAAa,WAAW,OAAO;CACrC,IAAI,CAAC,YAAY,MAAM,IAAI,MAAM,WAAW,KAAK,+BAA+B;CAChF,OAAO;AACT;AAIA,SAAS,KAAK,EACZ,OACA,cACA,eACA,kBACA,KACA,KACA,MACA,WACA,uBACA,aACA,UACA,MACA,QACA,QACA,kBACA,IACA,GAAG,QACuB;CAC1B,MAAM,QAAQ,WAAW,YAA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C;AACH;AAEA,MAAa,SAAS;CAAE;CAAM;CAAO;CAAS;CAAO;CAAW;AAAM"}
|
package/src/switch.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { PartComponentProps } from "./runtime.js";
|
|
2
|
+
import { ReactElement } from "react";
|
|
3
|
+
import { SwitchProps } from "@kirana-ui/core/switch";
|
|
4
|
+
export type RootProps = Omit<SwitchProps, "id"> & {
|
|
5
|
+
id?: string;
|
|
6
|
+
} & PartComponentProps;
|
|
7
|
+
declare function Root({ checked, defaultChecked, onCheckedChange, disabled, readOnly, required, name, value, id, children, ...rest }: RootProps): ReactElement;
|
|
8
|
+
declare const Thumb: (props: PartComponentProps) => ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
|
|
9
|
+
export declare const Switch: {
|
|
10
|
+
Root: typeof Root;
|
|
11
|
+
Thumb: typeof Thumb;
|
|
12
|
+
};
|
|
13
|
+
export {};
|
|
14
|
+
//# sourceMappingURL=switch.d.ts.map
|