@karni/ui 0.0.0-stage → 0.3.1
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 +201 -0
- package/README.md +288 -2
- package/dist/index.cjs +9 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.js +4764 -0
- package/dist/index.js.map +1 -0
- package/dist/manifest.json +85 -0
- package/dist/mockServiceWorker.js +361 -0
- package/dist/src/auth/AuthGuard.d.ts +12 -0
- package/dist/src/auth/LoginForm.d.ts +29 -0
- package/dist/src/auth/engine.d.ts +99 -0
- package/dist/src/auth/provider.d.ts +43 -0
- package/dist/src/auth/storage.d.ts +26 -0
- package/dist/src/auth/types.d.ts +32 -0
- package/dist/src/auth/usePermission.d.ts +7 -0
- package/dist/src/components/brand/karni-mark.d.ts +21 -0
- package/dist/src/components/brand/mark-paths.d.ts +8 -0
- package/dist/src/components/chart/data-chart.d.ts +74 -0
- package/dist/src/components/empty-state/empty-state.d.ts +20 -0
- package/dist/src/components/file-upload/FileUpload.d.ts +44 -0
- package/dist/src/components/layout/layout.d.ts +55 -0
- package/dist/src/components/modal/modal.d.ts +45 -0
- package/dist/src/components/page/page.d.ts +29 -0
- package/dist/src/components/primitives/button.d.ts +44 -0
- package/dist/src/components/primitives/checkbox.d.ts +5 -0
- package/dist/src/components/primitives/dropdown-menu.d.ts +24 -0
- package/dist/src/components/primitives/icons.d.ts +11 -0
- package/dist/src/components/primitives/input.d.ts +4 -0
- package/dist/src/components/primitives/label.d.ts +5 -0
- package/dist/src/components/primitives/select.d.ts +63 -0
- package/dist/src/components/primitives/skeleton.d.ts +4 -0
- package/dist/src/components/primitives/switch.d.ts +5 -0
- package/dist/src/components/primitives/tabs.d.ts +10 -0
- package/dist/src/components/primitives/textarea.d.ts +4 -0
- package/dist/src/components/resource/ResourceDetail.d.ts +7 -0
- package/dist/src/components/resource/ResourceForm.d.ts +7 -0
- package/dist/src/components/resource/ResourceSelect.d.ts +7 -0
- package/dist/src/components/resource/ResourceTable.d.ts +11 -0
- package/dist/src/components/resource/audit-fields.d.ts +17 -0
- package/dist/src/components/resource/detail/ResourceDetail.d.ts +30 -0
- package/dist/src/components/resource/displayLabel.d.ts +12 -0
- package/dist/src/components/resource/form/ResourceForm.d.ts +50 -0
- package/dist/src/components/resource/form/field-inputs.d.ts +20 -0
- package/dist/src/components/resource/form/fixtures.d.ts +24 -0
- package/dist/src/components/resource/form/prepare.d.ts +54 -0
- package/dist/src/components/resource/history/ChangeHistory.d.ts +18 -0
- package/dist/src/components/resource/import/ResourceImport.d.ts +18 -0
- package/dist/src/components/resource/select/ResourceSelect.d.ts +36 -0
- package/dist/src/components/resource/select/use-debounced-value.d.ts +2 -0
- package/dist/src/components/resource/table/BulkActionsBar.d.ts +20 -0
- package/dist/src/components/resource/table/ExportButtons.d.ts +17 -0
- package/dist/src/components/resource/table/FilterBar.d.ts +29 -0
- package/dist/src/components/resource/table/PaginationControls.d.ts +19 -0
- package/dist/src/components/resource/table/ResourceTable.d.ts +72 -0
- package/dist/src/components/resource/table/demo-fixtures.d.ts +25 -0
- package/dist/src/components/resource/table/derive-columns.d.ts +33 -0
- package/dist/src/components/resource/table/table-utils.d.ts +24 -0
- package/dist/src/components/resource/table/use-debounced-value.d.ts +1 -0
- package/dist/src/components/stat/stat.d.ts +24 -0
- package/dist/src/components/toast/toast-store.d.ts +36 -0
- package/dist/src/components/toast/toaster.d.ts +33 -0
- package/dist/src/components/toast/use-toast.d.ts +24 -0
- package/dist/src/hooks/keys.d.ts +24 -0
- package/dist/src/hooks/multipart.d.ts +44 -0
- package/dist/src/hooks/params.d.ts +25 -0
- package/dist/src/hooks/types.d.ts +41 -0
- package/dist/src/hooks/useChangeHistory.d.ts +29 -0
- package/dist/src/hooks/useResourceImport.d.ts +107 -0
- package/dist/src/hooks/useResourceItem.d.ts +4 -0
- package/dist/src/hooks/useResourceList.d.ts +4 -0
- package/dist/src/hooks/useResourceMutations.d.ts +13 -0
- package/dist/src/index.d.ts +95 -0
- package/dist/src/lib/cn.d.ts +8 -0
- package/dist/src/lib/roles.d.ts +45 -0
- package/dist/src/lib/variants.d.ts +33 -0
- package/dist/src/schema/client.d.ts +90 -0
- package/dist/src/schema/parse.d.ts +101 -0
- package/dist/src/schema/provider.d.ts +13 -0
- package/dist/src/schema/types.d.ts +84 -0
- package/dist/src/schema/useSchema.d.ts +17 -0
- package/dist/src/schema/zod.d.ts +4 -0
- package/dist/styles.css +87 -0
- package/dist/tailwind-preset.cjs +2 -0
- package/dist/tailwind-preset.cjs.map +1 -0
- package/dist/tailwind-preset.d.ts +58 -0
- package/dist/tailwind-preset.js +64 -0
- package/dist/tailwind-preset.js.map +1 -0
- package/eslint-plugin/index.js +72 -0
- package/eslint-plugin/lib/docs.js +11 -0
- package/eslint-plugin/lib/glob.js +51 -0
- package/eslint-plugin/rules/max-page-lines.js +65 -0
- package/eslint-plugin/rules/no-deep-imports.js +61 -0
- package/eslint-plugin/rules/no-direct-http.js +128 -0
- package/eslint-plugin/rules/no-raw-elements.js +65 -0
- package/eslint-plugin/rules.json +26 -0
- package/package.json +153 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,289 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @karni/ui
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Schema-driven React components for [Karni](https://github.com/cbsshekhawat18-lab/karni#readme) backends. Point it at a
|
|
4
|
+
Karni API's OpenAPI schema and get working tables, forms, detail views and auth —
|
|
5
|
+
plus the themable primitives they are built from — without regenerating boilerplate.
|
|
6
|
+
|
|
7
|
+
- **Resource components** — `ResourceTable`, `ResourceForm`, `ResourceDetail`,
|
|
8
|
+
`ResourceSelect` render themselves from the backend schema (`/api/schema/`).
|
|
9
|
+
- **Hooks** — `useResourceList`, `useResourceItem`, `useResourceMutations`,
|
|
10
|
+
`useSchema` on TanStack Query, with a shared `karniKeys` key factory.
|
|
11
|
+
- **Auth** — `AuthProvider`, `useAuth`, `AuthGuard`, `usePermission` for Karni's
|
|
12
|
+
JWT or session auth modes.
|
|
13
|
+
- **Primitives** — Button, Input, Textarea, Select, Checkbox, Switch, Label,
|
|
14
|
+
Tabs, DropdownMenu, Skeleton: Radix-based, token-themed, `forwardRef`,
|
|
15
|
+
`className`-mergeable.
|
|
16
|
+
|
|
17
|
+
The package ships **dual ESM/CJS** output plus `.d.ts` files: `import` resolves
|
|
18
|
+
`dist/index.js`, `require()` resolves `dist/index.cjs` (same for
|
|
19
|
+
`@karni/ui/tailwind-preset`, whose CJS form exposes the preset as `.default`).
|
|
20
|
+
The `@karni/ui/eslint-plugin` export stays ESM-only (flat config). Peer
|
|
21
|
+
dependencies are resolved by your bundler or by Node.
|
|
22
|
+
|
|
23
|
+
## Install
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
npm install @karni/ui @tanstack/react-query @tanstack/react-table react-hook-form @hookform/resolvers zod
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Peer dependencies: `react` / `react-dom` (18 or 19), `@tanstack/react-query` v5,
|
|
30
|
+
`@tanstack/react-table` v8, `react-hook-form` v7, `@hookform/resolvers` and `zod` v3.
|
|
31
|
+
The project also needs `tailwindcss` (v3.4+ or v4) as a build tool — a
|
|
32
|
+
devDependency, as `karni new` sets it up: it compiles the classes the
|
|
33
|
+
components use and is never imported at runtime, so it is not a peer.
|
|
34
|
+
|
|
35
|
+
## Quick start
|
|
36
|
+
|
|
37
|
+
```tsx
|
|
38
|
+
import { AuthProvider, KarniProvider, ResourceTable, createKarniClient } from "@karni/ui";
|
|
39
|
+
import "@karni/ui/styles.css";
|
|
40
|
+
|
|
41
|
+
const client = createKarniClient({ baseUrl: "/api" });
|
|
42
|
+
|
|
43
|
+
export function App() {
|
|
44
|
+
return (
|
|
45
|
+
<KarniProvider client={client}>
|
|
46
|
+
<AuthProvider>
|
|
47
|
+
<ResourceTable resource="events" />
|
|
48
|
+
</AuthProvider>
|
|
49
|
+
</KarniProvider>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
`KarniProvider` creates a TanStack `QueryClient` when you do not pass one
|
|
55
|
+
(`queryClient` prop is the escape hatch). `AuthProvider` reads the auth mode from
|
|
56
|
+
the schema root (`x-karni-auth-mode`); override it with `mode="jwt" | "session"`.
|
|
57
|
+
|
|
58
|
+
## Styling
|
|
59
|
+
|
|
60
|
+
All component colors resolve through `--karni-*` CSS custom properties defined in
|
|
61
|
+
`@karni/ui/styles.css` (light values on `:root`, dark values under `.dark`).
|
|
62
|
+
Import it once, then wire Tailwind so the token-backed utility classes
|
|
63
|
+
(`bg-primary`, `text-muted-foreground`, …) exist in your build.
|
|
64
|
+
|
|
65
|
+
### Tailwind v3 (preset)
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
// tailwind.config.ts
|
|
69
|
+
import karniPreset from "@karni/ui/tailwind-preset";
|
|
70
|
+
|
|
71
|
+
export default {
|
|
72
|
+
presets: [karniPreset],
|
|
73
|
+
content: [
|
|
74
|
+
"./src/**/*.{ts,tsx}",
|
|
75
|
+
// So Tailwind sees the classes used inside @karni/ui:
|
|
76
|
+
"./node_modules/@karni/ui/dist/**/*.js",
|
|
77
|
+
],
|
|
78
|
+
};
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
// main.tsx
|
|
83
|
+
import "@karni/ui/styles.css";
|
|
84
|
+
import "./index.css"; // your @tailwind base/components/utilities file
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Tailwind v4 (CSS-first recipe)
|
|
88
|
+
|
|
89
|
+
Tailwind v4 has no `presets` array; map the tokens in CSS instead:
|
|
90
|
+
|
|
91
|
+
```css
|
|
92
|
+
/* app.css */
|
|
93
|
+
@import "tailwindcss";
|
|
94
|
+
@import "@karni/ui/styles.css";
|
|
95
|
+
|
|
96
|
+
/* Scan the library's shipped output for class names. */
|
|
97
|
+
@source "../node_modules/@karni/ui/dist";
|
|
98
|
+
|
|
99
|
+
/* Dark mode is class-based: add `dark` to <html> (or any ancestor). */
|
|
100
|
+
@custom-variant dark (&:where(.dark, .dark *));
|
|
101
|
+
|
|
102
|
+
/* Map Tailwind color/radius theme slots onto the --karni-* tokens. */
|
|
103
|
+
@theme inline {
|
|
104
|
+
--color-background: hsl(var(--karni-background));
|
|
105
|
+
--color-foreground: hsl(var(--karni-foreground));
|
|
106
|
+
--color-card: hsl(var(--karni-card));
|
|
107
|
+
--color-card-foreground: hsl(var(--karni-card-foreground));
|
|
108
|
+
--color-popover: hsl(var(--karni-popover));
|
|
109
|
+
--color-popover-foreground: hsl(var(--karni-popover-foreground));
|
|
110
|
+
--color-primary: hsl(var(--karni-primary));
|
|
111
|
+
--color-primary-foreground: hsl(var(--karni-primary-foreground));
|
|
112
|
+
--color-secondary: hsl(var(--karni-secondary));
|
|
113
|
+
--color-secondary-foreground: hsl(var(--karni-secondary-foreground));
|
|
114
|
+
--color-muted: hsl(var(--karni-muted));
|
|
115
|
+
--color-muted-foreground: hsl(var(--karni-muted-foreground));
|
|
116
|
+
--color-accent: hsl(var(--karni-accent));
|
|
117
|
+
--color-accent-foreground: hsl(var(--karni-accent-foreground));
|
|
118
|
+
--color-destructive: hsl(var(--karni-destructive));
|
|
119
|
+
--color-destructive-foreground: hsl(var(--karni-destructive-foreground));
|
|
120
|
+
--color-border: hsl(var(--karni-border));
|
|
121
|
+
--color-input: hsl(var(--karni-input));
|
|
122
|
+
--color-ring: hsl(var(--karni-ring));
|
|
123
|
+
--color-chart-1: hsl(var(--karni-chart-1));
|
|
124
|
+
--color-chart-2: hsl(var(--karni-chart-2));
|
|
125
|
+
--color-chart-3: hsl(var(--karni-chart-3));
|
|
126
|
+
--color-chart-4: hsl(var(--karni-chart-4));
|
|
127
|
+
--color-chart-5: hsl(var(--karni-chart-5));
|
|
128
|
+
--radius-lg: var(--karni-radius);
|
|
129
|
+
--radius-md: calc(var(--karni-radius) - 2px);
|
|
130
|
+
--radius-sm: calc(var(--karni-radius) - 4px);
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Theming and dark mode
|
|
135
|
+
|
|
136
|
+
Override any token after importing the stylesheet — components pick it up
|
|
137
|
+
everywhere, in both Tailwind versions:
|
|
138
|
+
|
|
139
|
+
```css
|
|
140
|
+
:root {
|
|
141
|
+
--karni-primary: 262 83% 58%; /* HSL triplet, no hsl() wrapper */
|
|
142
|
+
--karni-radius: 0.75rem;
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Toggle dark mode by adding/removing the `dark` class on `<html>`. Chart series
|
|
147
|
+
colors are `--karni-chart-1` … `--karni-chart-5`.
|
|
148
|
+
|
|
149
|
+
## Resource components
|
|
150
|
+
|
|
151
|
+
Every resource component takes the backend's resource name (the router prefix)
|
|
152
|
+
and renders from the schema. They are defaults, not cages — each one exposes
|
|
153
|
+
override props, and you can always drop down to the hooks + primitives.
|
|
154
|
+
|
|
155
|
+
```tsx
|
|
156
|
+
// Table: server-driven pagination, sorting, search and filters
|
|
157
|
+
<ResourceTable
|
|
158
|
+
resource="events"
|
|
159
|
+
columns={[{ field: "title" }, { field: "starts_at" }]} // optional override
|
|
160
|
+
onRowClick={(row) => navigate(`/events/${row.id}`)}
|
|
161
|
+
/>
|
|
162
|
+
|
|
163
|
+
// Form: fields, labels, and Zod validation derived from the schema.
|
|
164
|
+
// No `id` → create; with `id` it loads the item and PATCHes dirty fields.
|
|
165
|
+
<ResourceForm
|
|
166
|
+
resource="events"
|
|
167
|
+
onSuccess={(created) => navigate(`/events/${created.id}`)}
|
|
168
|
+
fields={{ internal_notes: { hidden: true }, venue: { label: "Location" } }}
|
|
169
|
+
/>
|
|
170
|
+
|
|
171
|
+
// Detail + relation picker
|
|
172
|
+
<ResourceDetail resource="events" id={eventId} />
|
|
173
|
+
<ResourceSelect resource="venues" value={venueId} onChange={setVenueId} />
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Audit fields — `AUDIT_FIELDS`: `id`, `created_at`, `updated_at`, `created_by`,
|
|
177
|
+
`updated_by`, which every Karni resource carries — never lead the screen.
|
|
178
|
+
`ResourceTable` hides their columns by default (domain fields come first, in
|
|
179
|
+
schema order; rows are still keyed by `id`). Show one by naming it in `columns`
|
|
180
|
+
(`{ field: "created_at" }`), or show all of them after the domain columns with
|
|
181
|
+
`showAuditFields`. `ResourceDetail` lists them last, in a muted "Record" group,
|
|
182
|
+
and still honours `fields` overrides for them. `ResourceForm` never renders them
|
|
183
|
+
(they are read-only).
|
|
184
|
+
|
|
185
|
+
A create form starts from the schema's defaults (a model's `default=`), and a
|
|
186
|
+
cleared optional field is left out of the request rather than sent as `null`
|
|
187
|
+
(`null` is sent only for nullable fields).
|
|
188
|
+
|
|
189
|
+
Validation schemas are generated at runtime with `zodFor(schema, "create" | "update")`;
|
|
190
|
+
`displayLabel(obj)` picks a human label for relation objects.
|
|
191
|
+
|
|
192
|
+
## Hooks
|
|
193
|
+
|
|
194
|
+
```tsx
|
|
195
|
+
const { data, isLoading } = useResourceList<Event>("events", {
|
|
196
|
+
page: 1,
|
|
197
|
+
pageSize: 25,
|
|
198
|
+
ordering: "-starts_at",
|
|
199
|
+
search: "conf",
|
|
200
|
+
filters: { status: "published" },
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
const event = useResourceItem<Event>("events", id);
|
|
204
|
+
const { create, update, remove } = useResourceMutations<Event>("events");
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
List responses are DRF pages: `{ count, next, previous, results }`
|
|
208
|
+
(the `Paginated<T>` type). Mutations invalidate `karniKeys.resource(resource)`
|
|
209
|
+
on success; pass `invalidateKeys` to invalidate more. Use `karniKeys` for any
|
|
210
|
+
manual cache work so your keys and the library's keys never drift.
|
|
211
|
+
|
|
212
|
+
## Auth
|
|
213
|
+
|
|
214
|
+
```tsx
|
|
215
|
+
const { user, status, login, logout } = useAuth();
|
|
216
|
+
const { hasRole, isAuthenticated } = usePermission();
|
|
217
|
+
|
|
218
|
+
<AuthGuard roles={["admin"]} fallback={<LoginPage />}>
|
|
219
|
+
<AdminPanel />
|
|
220
|
+
</AuthGuard>;
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
In JWT mode the refresh token is stored via a pluggable `TokenStorage`; the
|
|
224
|
+
default is `localStorage` (simple, but readable by any XSS on your page — use
|
|
225
|
+
`createMemoryTokenStorage()` or your own adapter for stricter setups, at the
|
|
226
|
+
cost of logout-on-reload). Session mode uses cookies + CSRF headers and needs no
|
|
227
|
+
token storage.
|
|
228
|
+
|
|
229
|
+
## Primitives and escape hatches
|
|
230
|
+
|
|
231
|
+
Primitives are exported directly (`Button`, `Input`, `Select`, …) and follow
|
|
232
|
+
three rules:
|
|
233
|
+
|
|
234
|
+
1. **`className` always merges** via `cn()` (clsx + tailwind-merge), so your
|
|
235
|
+
classes win over the component's own.
|
|
236
|
+
2. **Refs are forwarded** to the underlying element.
|
|
237
|
+
3. **Composition over configuration** — e.g. `buttonVariants()` is exported to
|
|
238
|
+
style any element as a button, and the styled Radix parts
|
|
239
|
+
(`SelectRoot`/`SelectTrigger`/`SelectContent`/`SelectItem`, the
|
|
240
|
+
`DropdownMenu*` family, `Tabs*`) are exported for fully custom composition.
|
|
241
|
+
|
|
242
|
+
`Select` note: Radix reserves the empty string, so the composed `Select` maps
|
|
243
|
+
"no selection" through an internal `"__karni_empty__"` sentinel. Consumers only
|
|
244
|
+
ever see `null`; pass `clearable` to render a clear item that emits `null`.
|
|
245
|
+
Option values `""` and `"__karni_empty__"` are reserved.
|
|
246
|
+
|
|
247
|
+
The `variants()` helper (a dependency-free cva-style utility) and `cn()` are
|
|
248
|
+
exported for building your own components in the same style.
|
|
249
|
+
|
|
250
|
+
## Manifest for coding agents
|
|
251
|
+
|
|
252
|
+
The published package contains `dist/manifest.json` —
|
|
253
|
+
`{ "components": [...], "hooks": [...], "roles": {...} }` — generated from the
|
|
254
|
+
public barrel (and a static scan of the `registerComponentRole` call sites) at
|
|
255
|
+
build time. AI coding agents can read it (export path
|
|
256
|
+
`@karni/ui/manifest.json`) to discover what exists instead of crawling source.
|
|
257
|
+
|
|
258
|
+
## ESLint plugin
|
|
259
|
+
|
|
260
|
+
`@karni/ui/eslint-plugin` ships the Karni frontend enforcement rules
|
|
261
|
+
(`karni/no-raw-elements`, `karni/max-page-lines`, `karni/no-deep-imports`,
|
|
262
|
+
`karni/no-direct-http`) as a flat-config preset for ESLint >= 9:
|
|
263
|
+
|
|
264
|
+
```js
|
|
265
|
+
// eslint.config.js
|
|
266
|
+
import karni from "@karni/ui/eslint-plugin";
|
|
267
|
+
export default [...karni.configs.recommended];
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
Every rule is an error; opt-outs are disable comments with a REQUIRED reason
|
|
271
|
+
(`// eslint-disable-next-line karni/no-direct-http -- <reason>`). See
|
|
272
|
+
`docs/enforcement.md` at the repo root.
|
|
273
|
+
|
|
274
|
+
## Development
|
|
275
|
+
|
|
276
|
+
```sh
|
|
277
|
+
npm install
|
|
278
|
+
npm run build # tsc --noEmit && vite build && generate manifest
|
|
279
|
+
npm test # vitest (jsdom + testing-library + msw)
|
|
280
|
+
npm run lint # eslint 9 flat config (strict TS, no `any`, D7, karni/* dogfood)
|
|
281
|
+
npm run storybook # Storybook 8 (react-vite) with a light/dark toolbar
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
Before the first `npm run storybook`, generate the MSW worker once:
|
|
285
|
+
`npx msw init public/`.
|
|
286
|
+
|
|
287
|
+
The source of truth for color discipline is enforced by tests: no Tailwind
|
|
288
|
+
palette classes or raw color literals in component source — tokens only
|
|
289
|
+
(see `src/components/primitives/no-color-literals.test.ts`).
|