shadcn-settings 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/README.md +148 -0
- package/dist/components/field-shell.d.ts +24 -0
- package/dist/components/fields/select-field.d.ts +3 -0
- package/dist/components/fields/switch-field.d.ts +3 -0
- package/dist/components/fields/text-field.d.ts +5 -0
- package/dist/components/settings-field.d.ts +17 -0
- package/dist/components/settings-list.d.ts +33 -0
- package/dist/components/ui/select.d.ts +9 -0
- package/dist/components/ui/switch.d.ts +4 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.mjs +319 -0
- package/dist/lib/use-commit.d.ts +10 -0
- package/dist/lib/utils.d.ts +3 -0
- package/dist/types.d.ts +98 -0
- package/package.json +68 -0
package/README.md
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# shadcn-settings
|
|
2
|
+
|
|
3
|
+
Schema-driven **settings form renderer** for React, built on
|
|
4
|
+
[shadcn/ui](https://ui.shadcn.com) + Radix primitives.
|
|
5
|
+
|
|
6
|
+
Describe your settings as plain data (typically JSON), hand the list to
|
|
7
|
+
`<SettingsList />` with `getValue` / `onCommit` callbacks, and it renders the
|
|
8
|
+
controls for you — no per-field JSX. It knows nothing about *where* your values
|
|
9
|
+
live (localStorage, an API, a config file), so the same schema drives
|
|
10
|
+
rendering, persistence and search independently.
|
|
11
|
+
|
|
12
|
+
- **Field variants** — `string`, `password` (show/hide), `textarea`, `select`,
|
|
13
|
+
`switch`.
|
|
14
|
+
- **Layout variants** — `card`, `inline`, `ghost` per field.
|
|
15
|
+
- **Bring your own types** — inject extra `renderers` (e.g. a `theme` picker)
|
|
16
|
+
or override a built-in.
|
|
17
|
+
- **Optimistic + async commits** — edits fire `onChange` immediately and
|
|
18
|
+
`onCommit` on blur/change; async commits show a spinner automatically.
|
|
19
|
+
- **Style-agnostic** — ships shadcn token defaults, and every slot accepts a
|
|
20
|
+
`classNames` override so it matches your app's design.
|
|
21
|
+
|
|
22
|
+
## Install
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install shadcn-settings
|
|
26
|
+
# peer deps: react, react-dom
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Uses shadcn/ui theme tokens (`--border`, `--card`, `--primary`,
|
|
30
|
+
`--muted-foreground`, …). If your app already uses shadcn/ui, no extra setup is
|
|
31
|
+
needed.
|
|
32
|
+
|
|
33
|
+
## Quick start
|
|
34
|
+
|
|
35
|
+
```tsx
|
|
36
|
+
import { SettingsList, type SettingsFieldSchema } from "shadcn-settings";
|
|
37
|
+
|
|
38
|
+
const fields: SettingsFieldSchema[] = [
|
|
39
|
+
{
|
|
40
|
+
name: "Background Art",
|
|
41
|
+
key: "showBackgroundArt",
|
|
42
|
+
type: "switch",
|
|
43
|
+
description: "Show a random artistic background on the homepage.",
|
|
44
|
+
default: true,
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
name: "SearXNG URL",
|
|
48
|
+
key: "searxngURL",
|
|
49
|
+
type: "string",
|
|
50
|
+
placeholder: "http://localhost:4000",
|
|
51
|
+
},
|
|
52
|
+
];
|
|
53
|
+
|
|
54
|
+
export function Settings() {
|
|
55
|
+
return (
|
|
56
|
+
<SettingsList
|
|
57
|
+
fields={fields}
|
|
58
|
+
getValue={(field) => localStorage.getItem(field.key) ?? field.default}
|
|
59
|
+
onCommit={(field, value) =>
|
|
60
|
+
localStorage.setItem(field.key, String(value))
|
|
61
|
+
}
|
|
62
|
+
/>
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Schema
|
|
68
|
+
|
|
69
|
+
Each entry is a `SettingsFieldSchema`:
|
|
70
|
+
|
|
71
|
+
| key | type | notes |
|
|
72
|
+
| ------------- | ------------------------------------------- | --------------------------------------- |
|
|
73
|
+
| `name` | `string` | Label |
|
|
74
|
+
| `key` | `string` | Persistence key / React key |
|
|
75
|
+
| `type` | `"string" \| "password" \| "textarea" \| "select" \| "switch"` | or a custom type you register |
|
|
76
|
+
| `description` | `string?` | Helper text |
|
|
77
|
+
| `placeholder` | `string?` | Text inputs |
|
|
78
|
+
| `default` | `string \| boolean?` | Used when the value is `undefined` |
|
|
79
|
+
| `options` | `{ name, value }[]?` | `select` only |
|
|
80
|
+
| `links` | `{ name, url }[]?` | Reference links under the description |
|
|
81
|
+
|
|
82
|
+
Extra keys (`scope`, `env`, …) pass through untouched, so you can carry your own
|
|
83
|
+
metadata on each field.
|
|
84
|
+
|
|
85
|
+
## Rendering a single field
|
|
86
|
+
|
|
87
|
+
```tsx
|
|
88
|
+
import { SettingsField } from "shadcn-settings";
|
|
89
|
+
|
|
90
|
+
<SettingsField field={field} value={value} onCommit={save} variant="inline" />;
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Custom / overriding field types
|
|
94
|
+
|
|
95
|
+
Pass a `renderers` map. The special `"unknown"` key replaces the fallback used
|
|
96
|
+
for unregistered types.
|
|
97
|
+
|
|
98
|
+
```tsx
|
|
99
|
+
<SettingsList
|
|
100
|
+
fields={fields}
|
|
101
|
+
getValue={getValue}
|
|
102
|
+
onCommit={onCommit}
|
|
103
|
+
renderers={{
|
|
104
|
+
theme: ({ field, value, onCommit }) => (
|
|
105
|
+
<MyThemePicker value={value} onChange={onCommit} label={field.name} />
|
|
106
|
+
),
|
|
107
|
+
}}
|
|
108
|
+
/>
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
A custom renderer receives `SettingsFieldRenderProps`: `field`, `value`,
|
|
112
|
+
`onChange`, `onCommit`, `variant`, `anchorId`, `titleAddon`, `classNames`,
|
|
113
|
+
`disabled`.
|
|
114
|
+
|
|
115
|
+
## Matching your design
|
|
116
|
+
|
|
117
|
+
Every field accepts `classNames` overrides:
|
|
118
|
+
|
|
119
|
+
```tsx
|
|
120
|
+
<SettingsList
|
|
121
|
+
/* … */
|
|
122
|
+
variant="card"
|
|
123
|
+
classNames={{
|
|
124
|
+
root: "rounded-xl border-light-200 bg-light-primary/80 dark:border-dark-200",
|
|
125
|
+
title: "text-black dark:text-white",
|
|
126
|
+
description: "text-black/50 dark:text-white/50",
|
|
127
|
+
}}
|
|
128
|
+
/>
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Slots: `root`, `header`, `title`, `description`, `control`.
|
|
132
|
+
|
|
133
|
+
## API
|
|
134
|
+
|
|
135
|
+
- `SettingsList` — renders an ordered schema. Props: `fields`, `getValue`,
|
|
136
|
+
`onCommit`, `onChange?`, `variant?`, `renderers?`, `anchorId?`,
|
|
137
|
+
`renderTitleAddon?`, `classNames?`, `className?`.
|
|
138
|
+
- `SettingsField` — renders one field; dispatches on `field.type`.
|
|
139
|
+
- `FieldShell` — the label/description/control chrome, if you build a field
|
|
140
|
+
from scratch.
|
|
141
|
+
- `StringField`, `PasswordField`, `TextareaField`, `SelectField`,
|
|
142
|
+
`SwitchField` — the individual variants.
|
|
143
|
+
- `Select*`, `Switch` — the underlying shadcn/ui primitives.
|
|
144
|
+
- `useCommit` — the async-commit-with-spinner hook.
|
|
145
|
+
|
|
146
|
+
## License
|
|
147
|
+
|
|
148
|
+
rights.institute/PROSPER
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { type VariantProps } from "class-variance-authority";
|
|
3
|
+
import type { SettingsClassNames, SettingsFieldLink, SettingsFieldVariant } from "../types";
|
|
4
|
+
/**
|
|
5
|
+
* The visual chrome shared by every field: an optional bordered container, the
|
|
6
|
+
* label/description header, reference links, and a slot for the control. The
|
|
7
|
+
* `layout` prop decides where the control sits relative to the header.
|
|
8
|
+
*/
|
|
9
|
+
declare const shellVariants: (props?: ({
|
|
10
|
+
variant?: "card" | "inline" | "ghost" | null | undefined;
|
|
11
|
+
} & import("class-variance-authority/types").ClassProp) | undefined) => string;
|
|
12
|
+
export interface FieldShellProps extends VariantProps<typeof shellVariants> {
|
|
13
|
+
anchorId?: string;
|
|
14
|
+
title: string;
|
|
15
|
+
description?: string;
|
|
16
|
+
links?: SettingsFieldLink[];
|
|
17
|
+
titleAddon?: React.ReactNode;
|
|
18
|
+
classNames?: SettingsClassNames;
|
|
19
|
+
/** `stacked` puts the control under the header; `row` puts it beside it. */
|
|
20
|
+
layout?: "stacked" | "row";
|
|
21
|
+
children: React.ReactNode;
|
|
22
|
+
}
|
|
23
|
+
export declare function FieldShell({ anchorId, title, description, links, titleAddon, classNames, variant, layout, children, }: FieldShellProps): React.JSX.Element;
|
|
24
|
+
export type { SettingsFieldVariant };
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { SettingsFieldRenderProps } from "../../types";
|
|
2
|
+
/** Dropdown control for the `select` variant. Commits on change. */
|
|
3
|
+
export declare function SelectField({ field, value, onChange, onCommit, variant, anchorId, titleAddon, classNames, disabled, }: SettingsFieldRenderProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { SettingsFieldRenderProps } from "../../types";
|
|
2
|
+
/** Toggle control for the `switch` variant. Laid out as a row. Commits on change. */
|
|
3
|
+
export declare function SwitchField({ field, value, onChange, onCommit, variant, anchorId, titleAddon, classNames, disabled, }: SettingsFieldRenderProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type { SettingsFieldRenderProps } from "../../types";
|
|
3
|
+
export declare function StringField(props: SettingsFieldRenderProps): React.JSX.Element;
|
|
4
|
+
export declare function PasswordField(props: SettingsFieldRenderProps): React.JSX.Element;
|
|
5
|
+
export declare function TextareaField(props: SettingsFieldRenderProps): React.JSX.Element;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type { SettingsFieldRenderProps, SettingsFieldRenderers } from "../types";
|
|
3
|
+
/** The variants shipped with this package. */
|
|
4
|
+
export declare const builtinRenderers: SettingsFieldRenderers;
|
|
5
|
+
export interface SettingsFieldProps extends SettingsFieldRenderProps {
|
|
6
|
+
/**
|
|
7
|
+
* Extra or overriding renderers merged over the built-ins. Use the special
|
|
8
|
+
* `"unknown"` key to customise the fallback, or add a new `type` (e.g.
|
|
9
|
+
* `"theme"`) the schema uses.
|
|
10
|
+
*/
|
|
11
|
+
renderers?: SettingsFieldRenderers;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Renders a single settings field by dispatching on `field.type`. Unknown
|
|
15
|
+
* types fall back to the `unknown` renderer.
|
|
16
|
+
*/
|
|
17
|
+
export declare function SettingsField({ renderers, ...props }: SettingsFieldProps): React.JSX.Element;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import type { SettingsClassNames, SettingsFieldRenderers, SettingsFieldSchema, SettingsFieldVariant, SettingsValue } from "../types";
|
|
3
|
+
export interface SettingsListProps {
|
|
4
|
+
/** The fields to render, in order. */
|
|
5
|
+
fields: SettingsFieldSchema[];
|
|
6
|
+
/**
|
|
7
|
+
* Current value for a field. Return `undefined` to fall back to the field's
|
|
8
|
+
* own `default`. Called per-field so client/server-scoped reads can differ.
|
|
9
|
+
*/
|
|
10
|
+
getValue: (field: SettingsFieldSchema) => SettingsValue;
|
|
11
|
+
/** Persist a committed value for a field. May be async. */
|
|
12
|
+
onCommit: (field: SettingsFieldSchema, value: SettingsValue) => void | Promise<void>;
|
|
13
|
+
/** Optional optimistic per-field change handler (pre-commit). */
|
|
14
|
+
onChange?: (field: SettingsFieldSchema, value: SettingsValue) => void;
|
|
15
|
+
/** Layout variant applied to every field. */
|
|
16
|
+
variant?: SettingsFieldVariant;
|
|
17
|
+
/** Extra/override renderers (e.g. a `theme` variant). */
|
|
18
|
+
renderers?: SettingsFieldRenderers;
|
|
19
|
+
/** Build a DOM id for a field container (anchoring/deep links). */
|
|
20
|
+
anchorId?: (field: SettingsFieldSchema) => string;
|
|
21
|
+
/** Render a node beside a field's title (e.g. a copy-link button). */
|
|
22
|
+
renderTitleAddon?: (field: SettingsFieldSchema) => React.ReactNode;
|
|
23
|
+
/** Per-field class-name overrides. */
|
|
24
|
+
classNames?: SettingsClassNames;
|
|
25
|
+
/** Class name for the wrapping list container. */
|
|
26
|
+
className?: string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Renders an ordered list of settings fields from a schema. Each field's value
|
|
30
|
+
* and persistence are supplied by the host via `getValue`/`onCommit`, so this
|
|
31
|
+
* component stays agnostic about where values live (localStorage, an API, …).
|
|
32
|
+
*/
|
|
33
|
+
export declare function SettingsList({ fields, getValue, onCommit, onChange, variant, renderers, anchorId, renderTitleAddon, classNames, className, }: SettingsListProps): React.JSX.Element;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import * as SelectPrimitive from "@radix-ui/react-select";
|
|
3
|
+
/** shadcn/ui select built on `@radix-ui/react-select`. */
|
|
4
|
+
export declare const Select: React.FC<SelectPrimitive.SelectProps>;
|
|
5
|
+
export declare const SelectGroup: React.ForwardRefExoticComponent<SelectPrimitive.SelectGroupProps & React.RefAttributes<HTMLDivElement>>;
|
|
6
|
+
export declare const SelectValue: React.ForwardRefExoticComponent<SelectPrimitive.SelectValueProps & React.RefAttributes<HTMLSpanElement>>;
|
|
7
|
+
export declare const SelectTrigger: React.ForwardRefExoticComponent<Omit<SelectPrimitive.SelectTriggerProps & React.RefAttributes<HTMLButtonElement>, "ref"> & React.RefAttributes<HTMLButtonElement>>;
|
|
8
|
+
export declare const SelectContent: React.ForwardRefExoticComponent<Omit<SelectPrimitive.SelectContentProps & React.RefAttributes<HTMLDivElement>, "ref"> & React.RefAttributes<HTMLDivElement>>;
|
|
9
|
+
export declare const SelectItem: React.ForwardRefExoticComponent<Omit<SelectPrimitive.SelectItemProps & React.RefAttributes<HTMLDivElement>, "ref"> & React.RefAttributes<HTMLDivElement>>;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import * as SwitchPrimitive from "@radix-ui/react-switch";
|
|
3
|
+
/** shadcn/ui switch built on `@radix-ui/react-switch`. */
|
|
4
|
+
export declare const Switch: React.ForwardRefExoticComponent<Omit<SwitchPrimitive.SwitchProps & React.RefAttributes<HTMLButtonElement>, "ref"> & React.RefAttributes<HTMLButtonElement>>;
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use client";Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,s)=>(s=n==null?{}:e(i(n)),o(r||!n||!n.__esModule||!a.call(n,`default`)?t(s,`default`,{value:n,enumerable:!0}):s,n));let c=require("react");c=s(c,1);let l=require("clsx"),u=require("tailwind-merge"),d=require("class-variance-authority"),f=require("react/jsx-runtime"),p=require("lucide-react"),m=require("@radix-ui/react-select");m=s(m,1);let h=require("@radix-ui/react-switch");h=s(h,1);function g(...e){return(0,u.twMerge)((0,l.clsx)(e))}var _=(0,d.cva)(`scroll-mt-4 transition-colors`,{variants:{variant:{card:`rounded-xl border border-border bg-card/80 p-4 lg:p-6`,inline:`rounded-lg border border-border bg-card/50 px-4 py-3`,ghost:`py-2`}},defaultVariants:{variant:`card`}});function v({links:e}){return e?.length?(0,f.jsx)(`div`,{className:`mt-1 flex flex-wrap gap-2`,children:e.map(e=>(0,f.jsx)(`a`,{href:e.url,target:`_blank`,rel:`noopener noreferrer`,className:`text-[11px] text-blue-500 underline hover:text-blue-600 dark:text-blue-400 dark:hover:text-blue-300 lg:text-xs`,children:e.name},e.url))}):null}function y({title:e,description:t,links:n,titleAddon:r,classNames:i}){return(0,f.jsxs)(`div`,{className:g(i?.header),children:[(0,f.jsxs)(`h4`,{className:g(`flex items-center gap-1.5 text-sm text-foreground`,i?.title),children:[e,r]}),t?(0,f.jsx)(`p`,{className:g(`text-[11px] text-muted-foreground lg:text-xs`,i?.description),children:t}):null,(0,f.jsx)(v,{links:n})]})}function b({anchorId:e,title:t,description:n,links:r,titleAddon:i,classNames:a,variant:o,layout:s=`stacked`,children:c}){let l=(0,f.jsx)(y,{title:t,description:n,links:r,titleAddon:i,classNames:a});return(0,f.jsx)(`section`,{id:e,className:g(_({variant:o}),a?.root),children:s===`row`?(0,f.jsxs)(`div`,{className:`flex w-full flex-row items-center justify-between gap-3 lg:gap-5`,children:[l,(0,f.jsx)(`div`,{className:g(`flex-shrink-0`,a?.control),children:c})]}):(0,f.jsxs)(`div`,{className:`space-y-3 lg:space-y-5`,children:[l,(0,f.jsx)(`div`,{className:g(`relative`,a?.control),children:c})]})})}function x(e){let[t,n]=c.useState(!1),r=c.useRef(!0);return c.useEffect(()=>(r.current=!0,()=>{r.current=!1}),[]),{committing:t,commit:c.useCallback(async t=>{if(e){n(!0);try{await e(t)}finally{window.setTimeout(()=>{r.current&&n(!1)},150)}}},[e])}}var S=`w-full rounded-lg border border-input bg-background px-3 py-2 pr-10 !text-xs text-foreground placeholder:text-muted-foreground transition-colors focus-visible:outline-none focus-visible:border-ring disabled:cursor-not-allowed disabled:opacity-60 lg:px-4 lg:py-3 lg:!text-[13px]`;function C({field:e,value:t,onChange:n,onCommit:r,variant:i,anchorId:a,titleAddon:o,classNames:s,disabled:l,multiline:u=!1,secret:d=!1}){let{committing:m,commit:h}=x(r),[_,v]=c.useState(!1),y=typeof e.default==`string`?e.default:``,C=t??y,w=l||m,T=e=>n?.(e),E=e=>h(e);return(0,f.jsxs)(b,{anchorId:a,title:e.name,description:e.description,links:e.links,titleAddon:o,classNames:s,variant:i,children:[u?(0,f.jsx)(`textarea`,{value:C,onChange:e=>T(e.target.value),onBlur:e=>E(e.target.value),placeholder:e.placeholder,rows:4,disabled:w,className:S}):(0,f.jsx)(`input`,{value:C,onChange:e=>T(e.target.value),onBlur:e=>E(e.target.value),placeholder:e.placeholder,type:d&&!_?`password`:`text`,disabled:w,className:g(S,d&&`pr-16`)}),d&&!u?(0,f.jsx)(`button`,{type:`button`,onClick:()=>v(e=>!e),title:_?`Hide`:`Show`,className:`absolute right-3 top-1/2 -translate-y-1/2 p-1 text-muted-foreground transition-colors hover:text-foreground`,children:_?(0,f.jsx)(p.EyeOff,{className:`h-4 w-4`}):(0,f.jsx)(p.Eye,{className:`h-4 w-4`})}):null,m?(0,f.jsx)(`span`,{className:g(`pointer-events-none absolute text-muted-foreground`,u?`right-3 translate-y-3`:`top-1/2 -translate-y-1/2`,d&&!u?`right-8`:`right-3`),children:(0,f.jsx)(p.Loader2,{className:`h-4 w-4 animate-spin`})}):null]})}function w(e){return(0,f.jsx)(C,{...e})}function T(e){return(0,f.jsx)(C,{...e,secret:!0})}function E(e){return(0,f.jsx)(C,{...e,multiline:!0})}var D=m.Root,O=m.Group,k=m.Value,A=c.forwardRef(({className:e,children:t,...n},r)=>(0,f.jsxs)(m.Trigger,{ref:r,className:g(`flex h-9 w-full items-center justify-between whitespace-nowrap rounded-md border border-input bg-background px-3 py-2 text-sm shadow-sm`,`placeholder:text-muted-foreground focus:outline-none focus:ring-1 focus:ring-ring`,`disabled:cursor-not-allowed disabled:opacity-50 [&>span]:line-clamp-1`,e),...n,children:[t,(0,f.jsx)(m.Icon,{asChild:!0,children:(0,f.jsx)(p.ChevronDown,{className:`h-4 w-4 opacity-50`})})]}));A.displayName=m.Trigger.displayName;var j=c.forwardRef(({className:e,...t},n)=>(0,f.jsx)(m.ScrollUpButton,{ref:n,className:g(`flex cursor-default items-center justify-center py-1`,e),...t,children:(0,f.jsx)(p.ChevronUp,{className:`h-4 w-4`})}));j.displayName=m.ScrollUpButton.displayName;var M=c.forwardRef(({className:e,...t},n)=>(0,f.jsx)(m.ScrollDownButton,{ref:n,className:g(`flex cursor-default items-center justify-center py-1`,e),...t,children:(0,f.jsx)(p.ChevronDown,{className:`h-4 w-4`})}));M.displayName=m.ScrollDownButton.displayName;var N=c.forwardRef(({className:e,children:t,position:n=`popper`,...r},i)=>(0,f.jsx)(m.Portal,{children:(0,f.jsxs)(m.Content,{ref:i,className:g(`relative z-50 max-h-72 min-w-[8rem] overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-md`,n===`popper`&&`data-[side=bottom]:translate-y-1 data-[side=top]:-translate-y-1`,e),position:n,...r,children:[(0,f.jsx)(j,{}),(0,f.jsx)(m.Viewport,{className:g(`p-1`,n===`popper`&&`h-[var(--radix-select-trigger-height)] w-full min-w-[var(--radix-select-trigger-width)]`),children:t}),(0,f.jsx)(M,{})]})}));N.displayName=m.Content.displayName;var P=c.forwardRef(({className:e,children:t,...n},r)=>(0,f.jsxs)(m.Item,{ref:r,className:g(`relative flex w-full cursor-default select-none items-center rounded-sm py-1.5 pl-2 pr-8 text-sm outline-none`,`focus:bg-accent focus:text-accent-foreground`,`data-[disabled]:pointer-events-none data-[disabled]:opacity-50`,e),...n,children:[(0,f.jsx)(`span`,{className:`absolute right-2 flex h-3.5 w-3.5 items-center justify-center`,children:(0,f.jsx)(m.ItemIndicator,{children:(0,f.jsx)(p.Check,{className:`h-4 w-4`})})}),(0,f.jsx)(m.ItemText,{children:t})]}));P.displayName=m.Item.displayName;function F({field:e,value:t,onChange:n,onCommit:r,variant:i,anchorId:a,titleAddon:o,classNames:s,disabled:c}){let{committing:l,commit:u}=x(r),d=typeof e.default==`string`?e.default:void 0,p=t??d,m=(e.options??[]).filter(e=>e.value!==``);return(0,f.jsx)(b,{anchorId:a,title:e.name,description:e.description,links:e.links,titleAddon:o,classNames:s,variant:i,children:(0,f.jsxs)(D,{value:p,onValueChange:e=>{n?.(e),u(e)},disabled:c||l,children:[(0,f.jsx)(A,{className:`w-full`,children:(0,f.jsx)(k,{placeholder:`Select an option`})}),(0,f.jsx)(N,{children:m.map(e=>(0,f.jsx)(P,{value:e.value,children:e.name},e.value))})]})})}var I=c.forwardRef(({className:e,...t},n)=>(0,f.jsx)(h.Root,{ref:n,className:g(`peer inline-flex h-[1.15rem] w-8 shrink-0 cursor-pointer items-center rounded-full border border-transparent shadow-sm transition-colors outline-none`,`focus-visible:ring-2 focus-visible:ring-ring/50`,`data-[state=checked]:bg-primary data-[state=unchecked]:bg-input`,`disabled:cursor-not-allowed disabled:opacity-50`,e),...t,children:(0,f.jsx)(h.Thumb,{className:g(`pointer-events-none block size-4 rounded-full bg-background shadow-sm ring-0 transition-transform`,`data-[state=checked]:translate-x-[calc(100%-2px)] data-[state=unchecked]:translate-x-0`)})}));I.displayName=`Switch`;function L({field:e,value:t,onChange:n,onCommit:r,variant:i,anchorId:a,titleAddon:o,classNames:s,disabled:c}){let{committing:l,commit:u}=x(r),d=t===!0||t===`true`||t===void 0&&e.default===!0;return(0,f.jsx)(b,{anchorId:a,title:e.name,description:e.description,links:e.links,titleAddon:o,classNames:s,variant:i,layout:`row`,children:(0,f.jsx)(I,{checked:d,onCheckedChange:e=>{n?.(e),u(e)},disabled:c||l,className:`h-6 w-12`})})}var R={string:w,password:T,textarea:E,select:F,switch:L};function z({field:e,variant:t,anchorId:n,classNames:r}){return(0,f.jsx)(b,{anchorId:n,title:e.name,description:e.description,classNames:r,variant:t,children:(0,f.jsxs)(`p`,{className:`text-xs text-muted-foreground`,children:[`Unsupported field type: `,String(e.type)]})})}function B({renderers:e,...t}){let n=c.useMemo(()=>({...R,...e}),[e]),r=n[t.field.type]??n.unknown??z;return(0,f.jsx)(r,{...t})}function V({fields:e,getValue:t,onCommit:n,onChange:r,variant:i,renderers:a,anchorId:o,renderTitleAddon:s,classNames:c,className:l}){return(0,f.jsx)(`div`,{className:g(`flex-1 space-y-6 overflow-y-auto px-6 py-6`,l),children:e.map(e=>(0,f.jsx)(B,{field:e,value:t(e),onChange:r?t=>r(e,t):void 0,onCommit:t=>n(e,t),variant:i,renderers:a,anchorId:o?.(e),titleAddon:s?.(e),classNames:c},e.key))})}exports.FieldShell=b,exports.PasswordField=T,exports.Select=D,exports.SelectContent=N,exports.SelectField=F,exports.SelectGroup=O,exports.SelectItem=P,exports.SelectTrigger=A,exports.SelectValue=k,exports.SettingsField=B,exports.SettingsList=V,exports.StringField=w,exports.Switch=I,exports.SwitchField=L,exports.TextareaField=E,exports.builtinRenderers=R,exports.cn=g,exports.useCommit=x;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* shadcn-settings — schema-driven settings form renderer for shadcn/ui.
|
|
3
|
+
*
|
|
4
|
+
* Feed it a list of plain-data field declarations plus `getValue`/`onCommit`
|
|
5
|
+
* callbacks and it renders the controls (string, password, textarea, select,
|
|
6
|
+
* switch) with card / inline / ghost layout variants. Bring your own field
|
|
7
|
+
* types by passing extra `renderers`.
|
|
8
|
+
*/
|
|
9
|
+
export { SettingsList } from "./components/settings-list";
|
|
10
|
+
export type { SettingsListProps } from "./components/settings-list";
|
|
11
|
+
export { SettingsField, builtinRenderers } from "./components/settings-field";
|
|
12
|
+
export type { SettingsFieldProps } from "./components/settings-field";
|
|
13
|
+
export { FieldShell } from "./components/field-shell";
|
|
14
|
+
export type { FieldShellProps } from "./components/field-shell";
|
|
15
|
+
export { StringField, PasswordField, TextareaField, } from "./components/fields/text-field";
|
|
16
|
+
export { SelectField } from "./components/fields/select-field";
|
|
17
|
+
export { SwitchField } from "./components/fields/switch-field";
|
|
18
|
+
export * from "./components/ui/select";
|
|
19
|
+
export { Switch } from "./components/ui/switch";
|
|
20
|
+
export { cn } from "./lib/utils";
|
|
21
|
+
export { useCommit } from "./lib/use-commit";
|
|
22
|
+
export type { SettingsFieldType, SettingsFieldOption, SettingsFieldLink, SettingsFieldSchema, SettingsValue, SettingsFieldVariant, SettingsClassNames, SettingsFieldRenderProps, SettingsFieldRenderer, SettingsFieldRenderers, } from "./types";
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import * as e from "react";
|
|
3
|
+
import { clsx as t } from "clsx";
|
|
4
|
+
import { twMerge as n } from "tailwind-merge";
|
|
5
|
+
import { cva as r } from "class-variance-authority";
|
|
6
|
+
import { jsx as i, jsxs as a } from "react/jsx-runtime";
|
|
7
|
+
import { Check as o, ChevronDown as s, ChevronUp as c, Eye as l, EyeOff as u, Loader2 as d } from "lucide-react";
|
|
8
|
+
import * as f from "@radix-ui/react-select";
|
|
9
|
+
import * as p from "@radix-ui/react-switch";
|
|
10
|
+
//#region src/lib/utils.ts
|
|
11
|
+
function m(...e) {
|
|
12
|
+
return n(t(e));
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/components/field-shell.tsx
|
|
16
|
+
var h = r("scroll-mt-4 transition-colors", {
|
|
17
|
+
variants: { variant: {
|
|
18
|
+
card: "rounded-xl border border-border bg-card/80 p-4 lg:p-6",
|
|
19
|
+
inline: "rounded-lg border border-border bg-card/50 px-4 py-3",
|
|
20
|
+
ghost: "py-2"
|
|
21
|
+
} },
|
|
22
|
+
defaultVariants: { variant: "card" }
|
|
23
|
+
});
|
|
24
|
+
function g({ links: e }) {
|
|
25
|
+
return e?.length ? /* @__PURE__ */ i("div", {
|
|
26
|
+
className: "mt-1 flex flex-wrap gap-2",
|
|
27
|
+
children: e.map((e) => /* @__PURE__ */ i("a", {
|
|
28
|
+
href: e.url,
|
|
29
|
+
target: "_blank",
|
|
30
|
+
rel: "noopener noreferrer",
|
|
31
|
+
className: "text-[11px] text-blue-500 underline hover:text-blue-600 dark:text-blue-400 dark:hover:text-blue-300 lg:text-xs",
|
|
32
|
+
children: e.name
|
|
33
|
+
}, e.url))
|
|
34
|
+
}) : null;
|
|
35
|
+
}
|
|
36
|
+
function _({ title: e, description: t, links: n, titleAddon: r, classNames: o }) {
|
|
37
|
+
return /* @__PURE__ */ a("div", {
|
|
38
|
+
className: m(o?.header),
|
|
39
|
+
children: [
|
|
40
|
+
/* @__PURE__ */ a("h4", {
|
|
41
|
+
className: m("flex items-center gap-1.5 text-sm text-foreground", o?.title),
|
|
42
|
+
children: [e, r]
|
|
43
|
+
}),
|
|
44
|
+
t ? /* @__PURE__ */ i("p", {
|
|
45
|
+
className: m("text-[11px] text-muted-foreground lg:text-xs", o?.description),
|
|
46
|
+
children: t
|
|
47
|
+
}) : null,
|
|
48
|
+
/* @__PURE__ */ i(g, { links: n })
|
|
49
|
+
]
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
function v({ anchorId: e, title: t, description: n, links: r, titleAddon: o, classNames: s, variant: c, layout: l = "stacked", children: u }) {
|
|
53
|
+
let d = /* @__PURE__ */ i(_, {
|
|
54
|
+
title: t,
|
|
55
|
+
description: n,
|
|
56
|
+
links: r,
|
|
57
|
+
titleAddon: o,
|
|
58
|
+
classNames: s
|
|
59
|
+
});
|
|
60
|
+
return /* @__PURE__ */ i("section", {
|
|
61
|
+
id: e,
|
|
62
|
+
className: m(h({ variant: c }), s?.root),
|
|
63
|
+
children: l === "row" ? /* @__PURE__ */ a("div", {
|
|
64
|
+
className: "flex w-full flex-row items-center justify-between gap-3 lg:gap-5",
|
|
65
|
+
children: [d, /* @__PURE__ */ i("div", {
|
|
66
|
+
className: m("flex-shrink-0", s?.control),
|
|
67
|
+
children: u
|
|
68
|
+
})]
|
|
69
|
+
}) : /* @__PURE__ */ a("div", {
|
|
70
|
+
className: "space-y-3 lg:space-y-5",
|
|
71
|
+
children: [d, /* @__PURE__ */ i("div", {
|
|
72
|
+
className: m("relative", s?.control),
|
|
73
|
+
children: u
|
|
74
|
+
})]
|
|
75
|
+
})
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region src/lib/use-commit.ts
|
|
80
|
+
function y(t) {
|
|
81
|
+
let [n, r] = e.useState(!1), i = e.useRef(!0);
|
|
82
|
+
return e.useEffect(() => (i.current = !0, () => {
|
|
83
|
+
i.current = !1;
|
|
84
|
+
}), []), {
|
|
85
|
+
committing: n,
|
|
86
|
+
commit: e.useCallback(async (e) => {
|
|
87
|
+
if (t) {
|
|
88
|
+
r(!0);
|
|
89
|
+
try {
|
|
90
|
+
await t(e);
|
|
91
|
+
} finally {
|
|
92
|
+
window.setTimeout(() => {
|
|
93
|
+
i.current && r(!1);
|
|
94
|
+
}, 150);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}, [t])
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
//#endregion
|
|
101
|
+
//#region src/components/fields/text-field.tsx
|
|
102
|
+
var b = "w-full rounded-lg border border-input bg-background px-3 py-2 pr-10 !text-xs text-foreground placeholder:text-muted-foreground transition-colors focus-visible:outline-none focus-visible:border-ring disabled:cursor-not-allowed disabled:opacity-60 lg:px-4 lg:py-3 lg:!text-[13px]";
|
|
103
|
+
function x({ field: t, value: n, onChange: r, onCommit: o, variant: s, anchorId: c, titleAddon: f, classNames: p, disabled: h, multiline: g = !1, secret: _ = !1 }) {
|
|
104
|
+
let { committing: x, commit: S } = y(o), [C, w] = e.useState(!1), T = typeof t.default == "string" ? t.default : "", E = n ?? T, D = h || x, O = (e) => r?.(e), k = (e) => S(e);
|
|
105
|
+
return /* @__PURE__ */ a(v, {
|
|
106
|
+
anchorId: c,
|
|
107
|
+
title: t.name,
|
|
108
|
+
description: t.description,
|
|
109
|
+
links: t.links,
|
|
110
|
+
titleAddon: f,
|
|
111
|
+
classNames: p,
|
|
112
|
+
variant: s,
|
|
113
|
+
children: [
|
|
114
|
+
g ? /* @__PURE__ */ i("textarea", {
|
|
115
|
+
value: E,
|
|
116
|
+
onChange: (e) => O(e.target.value),
|
|
117
|
+
onBlur: (e) => k(e.target.value),
|
|
118
|
+
placeholder: t.placeholder,
|
|
119
|
+
rows: 4,
|
|
120
|
+
disabled: D,
|
|
121
|
+
className: b
|
|
122
|
+
}) : /* @__PURE__ */ i("input", {
|
|
123
|
+
value: E,
|
|
124
|
+
onChange: (e) => O(e.target.value),
|
|
125
|
+
onBlur: (e) => k(e.target.value),
|
|
126
|
+
placeholder: t.placeholder,
|
|
127
|
+
type: _ && !C ? "password" : "text",
|
|
128
|
+
disabled: D,
|
|
129
|
+
className: m(b, _ && "pr-16")
|
|
130
|
+
}),
|
|
131
|
+
_ && !g ? /* @__PURE__ */ i("button", {
|
|
132
|
+
type: "button",
|
|
133
|
+
onClick: () => w((e) => !e),
|
|
134
|
+
title: C ? "Hide" : "Show",
|
|
135
|
+
className: "absolute right-3 top-1/2 -translate-y-1/2 p-1 text-muted-foreground transition-colors hover:text-foreground",
|
|
136
|
+
children: i(C ? u : l, { className: "h-4 w-4" })
|
|
137
|
+
}) : null,
|
|
138
|
+
x ? /* @__PURE__ */ i("span", {
|
|
139
|
+
className: m("pointer-events-none absolute text-muted-foreground", g ? "right-3 translate-y-3" : "top-1/2 -translate-y-1/2", _ && !g ? "right-8" : "right-3"),
|
|
140
|
+
children: /* @__PURE__ */ i(d, { className: "h-4 w-4 animate-spin" })
|
|
141
|
+
}) : null
|
|
142
|
+
]
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
function S(e) {
|
|
146
|
+
return /* @__PURE__ */ i(x, { ...e });
|
|
147
|
+
}
|
|
148
|
+
function C(e) {
|
|
149
|
+
return /* @__PURE__ */ i(x, {
|
|
150
|
+
...e,
|
|
151
|
+
secret: !0
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
function w(e) {
|
|
155
|
+
return /* @__PURE__ */ i(x, {
|
|
156
|
+
...e,
|
|
157
|
+
multiline: !0
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
//#endregion
|
|
161
|
+
//#region src/components/ui/select.tsx
|
|
162
|
+
var T = f.Root, E = f.Group, D = f.Value, O = e.forwardRef(({ className: e, children: t, ...n }, r) => /* @__PURE__ */ a(f.Trigger, {
|
|
163
|
+
ref: r,
|
|
164
|
+
className: m("flex h-9 w-full items-center justify-between whitespace-nowrap rounded-md border border-input bg-background px-3 py-2 text-sm shadow-sm", "placeholder:text-muted-foreground focus:outline-none focus:ring-1 focus:ring-ring", "disabled:cursor-not-allowed disabled:opacity-50 [&>span]:line-clamp-1", e),
|
|
165
|
+
...n,
|
|
166
|
+
children: [t, /* @__PURE__ */ i(f.Icon, {
|
|
167
|
+
asChild: !0,
|
|
168
|
+
children: /* @__PURE__ */ i(s, { className: "h-4 w-4 opacity-50" })
|
|
169
|
+
})]
|
|
170
|
+
}));
|
|
171
|
+
O.displayName = f.Trigger.displayName;
|
|
172
|
+
var k = e.forwardRef(({ className: e, ...t }, n) => /* @__PURE__ */ i(f.ScrollUpButton, {
|
|
173
|
+
ref: n,
|
|
174
|
+
className: m("flex cursor-default items-center justify-center py-1", e),
|
|
175
|
+
...t,
|
|
176
|
+
children: /* @__PURE__ */ i(c, { className: "h-4 w-4" })
|
|
177
|
+
}));
|
|
178
|
+
k.displayName = f.ScrollUpButton.displayName;
|
|
179
|
+
var A = e.forwardRef(({ className: e, ...t }, n) => /* @__PURE__ */ i(f.ScrollDownButton, {
|
|
180
|
+
ref: n,
|
|
181
|
+
className: m("flex cursor-default items-center justify-center py-1", e),
|
|
182
|
+
...t,
|
|
183
|
+
children: /* @__PURE__ */ i(s, { className: "h-4 w-4" })
|
|
184
|
+
}));
|
|
185
|
+
A.displayName = f.ScrollDownButton.displayName;
|
|
186
|
+
var j = e.forwardRef(({ className: e, children: t, position: n = "popper", ...r }, o) => /* @__PURE__ */ i(f.Portal, { children: /* @__PURE__ */ a(f.Content, {
|
|
187
|
+
ref: o,
|
|
188
|
+
className: m("relative z-50 max-h-72 min-w-[8rem] overflow-hidden rounded-md border bg-popover text-popover-foreground shadow-md", n === "popper" && "data-[side=bottom]:translate-y-1 data-[side=top]:-translate-y-1", e),
|
|
189
|
+
position: n,
|
|
190
|
+
...r,
|
|
191
|
+
children: [
|
|
192
|
+
/* @__PURE__ */ i(k, {}),
|
|
193
|
+
/* @__PURE__ */ i(f.Viewport, {
|
|
194
|
+
className: m("p-1", n === "popper" && "h-[var(--radix-select-trigger-height)] w-full min-w-[var(--radix-select-trigger-width)]"),
|
|
195
|
+
children: t
|
|
196
|
+
}),
|
|
197
|
+
/* @__PURE__ */ i(A, {})
|
|
198
|
+
]
|
|
199
|
+
}) }));
|
|
200
|
+
j.displayName = f.Content.displayName;
|
|
201
|
+
var M = e.forwardRef(({ className: e, children: t, ...n }, r) => /* @__PURE__ */ a(f.Item, {
|
|
202
|
+
ref: r,
|
|
203
|
+
className: m("relative flex w-full cursor-default select-none items-center rounded-sm py-1.5 pl-2 pr-8 text-sm outline-none", "focus:bg-accent focus:text-accent-foreground", "data-[disabled]:pointer-events-none data-[disabled]:opacity-50", e),
|
|
204
|
+
...n,
|
|
205
|
+
children: [/* @__PURE__ */ i("span", {
|
|
206
|
+
className: "absolute right-2 flex h-3.5 w-3.5 items-center justify-center",
|
|
207
|
+
children: /* @__PURE__ */ i(f.ItemIndicator, { children: /* @__PURE__ */ i(o, { className: "h-4 w-4" }) })
|
|
208
|
+
}), /* @__PURE__ */ i(f.ItemText, { children: t })]
|
|
209
|
+
}));
|
|
210
|
+
M.displayName = f.Item.displayName;
|
|
211
|
+
//#endregion
|
|
212
|
+
//#region src/components/fields/select-field.tsx
|
|
213
|
+
function N({ field: e, value: t, onChange: n, onCommit: r, variant: o, anchorId: s, titleAddon: c, classNames: l, disabled: u }) {
|
|
214
|
+
let { committing: d, commit: f } = y(r), p = typeof e.default == "string" ? e.default : void 0, m = t ?? p, h = (e.options ?? []).filter((e) => e.value !== "");
|
|
215
|
+
return /* @__PURE__ */ i(v, {
|
|
216
|
+
anchorId: s,
|
|
217
|
+
title: e.name,
|
|
218
|
+
description: e.description,
|
|
219
|
+
links: e.links,
|
|
220
|
+
titleAddon: c,
|
|
221
|
+
classNames: l,
|
|
222
|
+
variant: o,
|
|
223
|
+
children: /* @__PURE__ */ a(T, {
|
|
224
|
+
value: m,
|
|
225
|
+
onValueChange: (e) => {
|
|
226
|
+
n?.(e), f(e);
|
|
227
|
+
},
|
|
228
|
+
disabled: u || d,
|
|
229
|
+
children: [/* @__PURE__ */ i(O, {
|
|
230
|
+
className: "w-full",
|
|
231
|
+
children: /* @__PURE__ */ i(D, { placeholder: "Select an option" })
|
|
232
|
+
}), /* @__PURE__ */ i(j, { children: h.map((e) => /* @__PURE__ */ i(M, {
|
|
233
|
+
value: e.value,
|
|
234
|
+
children: e.name
|
|
235
|
+
}, e.value)) })]
|
|
236
|
+
})
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
//#endregion
|
|
240
|
+
//#region src/components/ui/switch.tsx
|
|
241
|
+
var P = e.forwardRef(({ className: e, ...t }, n) => /* @__PURE__ */ i(p.Root, {
|
|
242
|
+
ref: n,
|
|
243
|
+
className: m("peer inline-flex h-[1.15rem] w-8 shrink-0 cursor-pointer items-center rounded-full border border-transparent shadow-sm transition-colors outline-none", "focus-visible:ring-2 focus-visible:ring-ring/50", "data-[state=checked]:bg-primary data-[state=unchecked]:bg-input", "disabled:cursor-not-allowed disabled:opacity-50", e),
|
|
244
|
+
...t,
|
|
245
|
+
children: /* @__PURE__ */ i(p.Thumb, { className: m("pointer-events-none block size-4 rounded-full bg-background shadow-sm ring-0 transition-transform", "data-[state=checked]:translate-x-[calc(100%-2px)] data-[state=unchecked]:translate-x-0") })
|
|
246
|
+
}));
|
|
247
|
+
P.displayName = "Switch";
|
|
248
|
+
//#endregion
|
|
249
|
+
//#region src/components/fields/switch-field.tsx
|
|
250
|
+
function F({ field: e, value: t, onChange: n, onCommit: r, variant: a, anchorId: o, titleAddon: s, classNames: c, disabled: l }) {
|
|
251
|
+
let { committing: u, commit: d } = y(r), f = t === !0 || t === "true" || t === void 0 && e.default === !0;
|
|
252
|
+
return /* @__PURE__ */ i(v, {
|
|
253
|
+
anchorId: o,
|
|
254
|
+
title: e.name,
|
|
255
|
+
description: e.description,
|
|
256
|
+
links: e.links,
|
|
257
|
+
titleAddon: s,
|
|
258
|
+
classNames: c,
|
|
259
|
+
variant: a,
|
|
260
|
+
layout: "row",
|
|
261
|
+
children: /* @__PURE__ */ i(P, {
|
|
262
|
+
checked: f,
|
|
263
|
+
onCheckedChange: (e) => {
|
|
264
|
+
n?.(e), d(e);
|
|
265
|
+
},
|
|
266
|
+
disabled: l || u,
|
|
267
|
+
className: "h-6 w-12"
|
|
268
|
+
})
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
//#endregion
|
|
272
|
+
//#region src/components/settings-field.tsx
|
|
273
|
+
var I = {
|
|
274
|
+
string: S,
|
|
275
|
+
password: C,
|
|
276
|
+
textarea: w,
|
|
277
|
+
select: N,
|
|
278
|
+
switch: F
|
|
279
|
+
};
|
|
280
|
+
function L({ field: e, variant: t, anchorId: n, classNames: r }) {
|
|
281
|
+
return /* @__PURE__ */ i(v, {
|
|
282
|
+
anchorId: n,
|
|
283
|
+
title: e.name,
|
|
284
|
+
description: e.description,
|
|
285
|
+
classNames: r,
|
|
286
|
+
variant: t,
|
|
287
|
+
children: /* @__PURE__ */ a("p", {
|
|
288
|
+
className: "text-xs text-muted-foreground",
|
|
289
|
+
children: ["Unsupported field type: ", String(e.type)]
|
|
290
|
+
})
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
function R({ renderers: t, ...n }) {
|
|
294
|
+
let r = e.useMemo(() => ({
|
|
295
|
+
...I,
|
|
296
|
+
...t
|
|
297
|
+
}), [t]), a = r[n.field.type] ?? r.unknown ?? L;
|
|
298
|
+
return /* @__PURE__ */ i(a, { ...n });
|
|
299
|
+
}
|
|
300
|
+
//#endregion
|
|
301
|
+
//#region src/components/settings-list.tsx
|
|
302
|
+
function z({ fields: e, getValue: t, onCommit: n, onChange: r, variant: a, renderers: o, anchorId: s, renderTitleAddon: c, classNames: l, className: u }) {
|
|
303
|
+
return /* @__PURE__ */ i("div", {
|
|
304
|
+
className: m("flex-1 space-y-6 overflow-y-auto px-6 py-6", u),
|
|
305
|
+
children: e.map((e) => /* @__PURE__ */ i(R, {
|
|
306
|
+
field: e,
|
|
307
|
+
value: t(e),
|
|
308
|
+
onChange: r ? (t) => r(e, t) : void 0,
|
|
309
|
+
onCommit: (t) => n(e, t),
|
|
310
|
+
variant: a,
|
|
311
|
+
renderers: o,
|
|
312
|
+
anchorId: s?.(e),
|
|
313
|
+
titleAddon: c?.(e),
|
|
314
|
+
classNames: l
|
|
315
|
+
}, e.key))
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
//#endregion
|
|
319
|
+
export { v as FieldShell, C as PasswordField, T as Select, j as SelectContent, N as SelectField, E as SelectGroup, M as SelectItem, O as SelectTrigger, D as SelectValue, R as SettingsField, z as SettingsList, S as StringField, P as Switch, F as SwitchField, w as TextareaField, I as builtinRenderers, m as cn, y as useCommit };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { SettingsValue } from "../types";
|
|
2
|
+
/**
|
|
3
|
+
* Wraps an `onCommit` callback with a `committing` flag so field controls can
|
|
4
|
+
* show a spinner / disable themselves while an async save is in flight. Safe
|
|
5
|
+
* against unmounts and tolerant of a synchronous (void) callback.
|
|
6
|
+
*/
|
|
7
|
+
export declare function useCommit(onCommit?: (value: SettingsValue) => void | Promise<void>): {
|
|
8
|
+
committing: boolean;
|
|
9
|
+
commit: (value: SettingsValue) => Promise<void>;
|
|
10
|
+
};
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type { ComponentType, ReactNode } from "react";
|
|
2
|
+
/** The built-in control variants this package knows how to render. */
|
|
3
|
+
export type SettingsFieldType = "string" | "password" | "textarea" | "select" | "switch";
|
|
4
|
+
/** An option in a `select` field. */
|
|
5
|
+
export interface SettingsFieldOption {
|
|
6
|
+
name: string;
|
|
7
|
+
value: string;
|
|
8
|
+
}
|
|
9
|
+
/** A labelled external link rendered under a field's description. */
|
|
10
|
+
export interface SettingsFieldLink {
|
|
11
|
+
name: string;
|
|
12
|
+
url: string;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* A declarative description of one setting. Schemas are plain data (usually
|
|
16
|
+
* JSON) so the same field list can drive rendering, persistence and search.
|
|
17
|
+
*
|
|
18
|
+
* `type` is typed as `string` rather than the built-in union so a host app can
|
|
19
|
+
* declare custom field types and supply matching renderers via
|
|
20
|
+
* {@link SettingsFieldRenderers}; unknown types fall back to the
|
|
21
|
+
* `unknown`-type renderer.
|
|
22
|
+
*/
|
|
23
|
+
export interface SettingsFieldSchema {
|
|
24
|
+
/** Human-readable label. */
|
|
25
|
+
name: string;
|
|
26
|
+
/** Stable key used for persistence and as the React key. */
|
|
27
|
+
key: string;
|
|
28
|
+
/** Control variant, e.g. `"string"` | `"select"` | a custom type. */
|
|
29
|
+
type: SettingsFieldType | (string & {});
|
|
30
|
+
/** Short helper text shown under the label. */
|
|
31
|
+
description?: string;
|
|
32
|
+
/** Placeholder for text-like inputs. */
|
|
33
|
+
placeholder?: string;
|
|
34
|
+
/** Default value used when no value has been set. */
|
|
35
|
+
default?: string | boolean;
|
|
36
|
+
/** Options for `select` fields. */
|
|
37
|
+
options?: SettingsFieldOption[];
|
|
38
|
+
/** Reference links rendered under the description. */
|
|
39
|
+
links?: SettingsFieldLink[];
|
|
40
|
+
/** Whether the field must be filled (advisory; not enforced here). */
|
|
41
|
+
required?: boolean;
|
|
42
|
+
/** Free-form scope tag carried through untouched (e.g. client/server). */
|
|
43
|
+
scope?: string;
|
|
44
|
+
/** Allow schema authors to attach extra metadata. */
|
|
45
|
+
[extra: string]: unknown;
|
|
46
|
+
}
|
|
47
|
+
/** The value types a field control can hold. */
|
|
48
|
+
export type SettingsValue = string | boolean | number | undefined;
|
|
49
|
+
/** Visual layout of a field. */
|
|
50
|
+
export type SettingsFieldVariant = "card" | "inline" | "ghost";
|
|
51
|
+
/** Per-slot class-name overrides so a host app can match its own design. */
|
|
52
|
+
export interface SettingsClassNames {
|
|
53
|
+
/** The outer field container. */
|
|
54
|
+
root?: string;
|
|
55
|
+
/** The label/description header block. */
|
|
56
|
+
header?: string;
|
|
57
|
+
/** The label text. */
|
|
58
|
+
title?: string;
|
|
59
|
+
/** The description text. */
|
|
60
|
+
description?: string;
|
|
61
|
+
/** The control (input/select/switch) wrapper. */
|
|
62
|
+
control?: string;
|
|
63
|
+
}
|
|
64
|
+
/** Props every field renderer receives from the dispatcher. */
|
|
65
|
+
export interface SettingsFieldRenderProps<F extends SettingsFieldSchema = SettingsFieldSchema> {
|
|
66
|
+
field: F;
|
|
67
|
+
/** Current value (falls back to `field.default` when undefined). */
|
|
68
|
+
value: SettingsValue;
|
|
69
|
+
/**
|
|
70
|
+
* Optimistic local update — fires on every keystroke/toggle so controlled
|
|
71
|
+
* hosts can mirror the value immediately.
|
|
72
|
+
*/
|
|
73
|
+
onChange?: (value: SettingsValue) => void;
|
|
74
|
+
/**
|
|
75
|
+
* Persist a value. May be async; the field shows a spinner while the
|
|
76
|
+
* returned promise is pending. Text inputs commit on blur, selects/switches
|
|
77
|
+
* commit immediately.
|
|
78
|
+
*/
|
|
79
|
+
onCommit?: (value: SettingsValue) => void | Promise<void>;
|
|
80
|
+
/** Layout variant. */
|
|
81
|
+
variant?: SettingsFieldVariant;
|
|
82
|
+
/** Stable DOM id for the field container (anchoring/deep links). */
|
|
83
|
+
anchorId?: string;
|
|
84
|
+
/** Extra node rendered next to the title (e.g. a copy-link button). */
|
|
85
|
+
titleAddon?: ReactNode;
|
|
86
|
+
/** Class-name overrides. */
|
|
87
|
+
classNames?: SettingsClassNames;
|
|
88
|
+
/** Disable the control. */
|
|
89
|
+
disabled?: boolean;
|
|
90
|
+
}
|
|
91
|
+
/** A component that renders one field type. */
|
|
92
|
+
export type SettingsFieldRenderer<F extends SettingsFieldSchema = SettingsFieldSchema> = ComponentType<SettingsFieldRenderProps<F>>;
|
|
93
|
+
/**
|
|
94
|
+
* Map of field `type` → renderer. Merged over the built-ins, so a host can
|
|
95
|
+
* override a built-in variant or add a brand-new one (e.g. `"theme"`). The
|
|
96
|
+
* special `"unknown"` key handles any type with no registered renderer.
|
|
97
|
+
*/
|
|
98
|
+
export type SettingsFieldRenderers = Record<string, SettingsFieldRenderer>;
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "shadcn-settings",
|
|
3
|
+
"description": "Prop-driven, schema-based settings form renderer built on shadcn/ui — string, password, textarea, select and switch field variants with card, inline and ghost layouts",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"author": "vtempest",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/OpenSourceAGI/qwksearch-research-agent.git",
|
|
9
|
+
"directory": "packages/shadcn-settings"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "./dist/index.cjs",
|
|
13
|
+
"module": "./dist/index.mjs",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"import": "./dist/index.mjs",
|
|
19
|
+
"require": "./dist/index.cjs"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist",
|
|
24
|
+
"README.md"
|
|
25
|
+
],
|
|
26
|
+
"sideEffects": false,
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "vite build && tsc --project tsconfig.build.json",
|
|
29
|
+
"type-check": "tsc --noEmit",
|
|
30
|
+
"prepublishOnly": "bun run build",
|
|
31
|
+
"test": "vitest run",
|
|
32
|
+
"test:coverage": "vitest run --coverage"
|
|
33
|
+
},
|
|
34
|
+
"keywords": [
|
|
35
|
+
"settings",
|
|
36
|
+
"preferences",
|
|
37
|
+
"config-form",
|
|
38
|
+
"schema-driven",
|
|
39
|
+
"shadcn",
|
|
40
|
+
"react"
|
|
41
|
+
],
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"react": ">=18",
|
|
44
|
+
"react-dom": ">=18"
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"@radix-ui/react-select": "^2.3.0",
|
|
48
|
+
"@radix-ui/react-switch": "^1.3.0",
|
|
49
|
+
"class-variance-authority": "^0.7.1",
|
|
50
|
+
"clsx": "^2.1.1",
|
|
51
|
+
"lucide-react": "^1.17.0",
|
|
52
|
+
"tailwind-merge": "^3.0.0"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@testing-library/react": "^16.1.0",
|
|
56
|
+
"@types/react": "^19.0.0",
|
|
57
|
+
"@types/react-dom": "^19.0.0",
|
|
58
|
+
"@vitejs/plugin-react": "^6.0.3",
|
|
59
|
+
"@vitest/coverage-v8": "^4.0.18",
|
|
60
|
+
"jsdom": "^28.1.0",
|
|
61
|
+
"react": "^19.2.7",
|
|
62
|
+
"react-dom": "^19.2.7",
|
|
63
|
+
"typescript": "^5.7.2",
|
|
64
|
+
"vite": "^8.1.3",
|
|
65
|
+
"vitest": "^4.0.18"
|
|
66
|
+
},
|
|
67
|
+
"license": "rights.institute/PROSPER"
|
|
68
|
+
}
|