@otwdesign/admin-ui 0.0.0-stage → 1.0.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 ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/NOTICE ADDED
@@ -0,0 +1,2 @@
1
+ OTW CMS
2
+ Copyright 2026 OTW SRL
package/README.md CHANGED
@@ -1,3 +1,13 @@
1
- # Temporary Holding Version
1
+ # @otwdesign/admin-ui
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ React components for OTW CMS admin screens, in the style of WordPress's admin: list tables,
4
+ postboxes, form fields, notices and buttons. Plugins build their own admin screens with them.
5
+
6
+ ```sh
7
+ npm install --save-dev @otwdesign/admin-ui
8
+ ```
9
+
10
+ Like `@otwdesign/sdk`, a plugin lists it as a peer dependency (`"@otwdesign/admin-ui": "^1.0.0"`).
11
+
12
+ Part of [OTW CMS](https://github.com/OTWSRL/otw-cms), a WordPress-style CMS for Next.js and
13
+ PostgreSQL. Licensed under the Apache License 2.0.
package/package.json CHANGED
@@ -1,6 +1,52 @@
1
1
  {
2
2
  "name": "@otwdesign/admin-ui",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "1.0.0",
4
+ "description": "React components for OTW CMS admin screens: list tables, postboxes, forms, notices and buttons.",
5
+ "license": "Apache-2.0",
6
+ "author": "OTW SRL",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/OTWSRL/otw-cms.git",
10
+ "directory": "packages/admin-ui"
11
+ },
12
+ "homepage": "https://github.com/OTWSRL/otw-cms/tree/main/packages/admin-ui#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/OTWSRL/otw-cms/issues"
15
+ },
16
+ "keywords": [
17
+ "otw-cms",
18
+ "cms",
19
+ "admin",
20
+ "react"
21
+ ],
22
+ "type": "module",
23
+ "exports": {
24
+ ".": "./src/index.ts"
25
+ },
26
+ "files": [
27
+ "src",
28
+ "NOTICE"
29
+ ],
30
+ "publishConfig": {
31
+ "access": "public"
32
+ },
33
+ "engines": {
34
+ "node": ">=22.18"
35
+ },
36
+ "dependencies": {
37
+ "@otwdesign/sdk": "1.0.0"
38
+ },
39
+ "peerDependencies": {
40
+ "react": "^19.0.0"
41
+ },
42
+ "devDependencies": {
43
+ "@types/react": "^19.3.0",
44
+ "@types/react-dom": "^19.2.0",
45
+ "happy-dom": "^21.0.4",
46
+ "react": "^19.3.1",
47
+ "react-dom": "^19.3.1"
48
+ },
49
+ "scripts": {
50
+ "typecheck": "tsc --noEmit -p ."
51
+ }
6
52
  }
package/src/boxes.tsx ADDED
@@ -0,0 +1,90 @@
1
+ import type { ReactNode } from 'react'
2
+ import { cx } from './button'
3
+ import { DismissButton } from './dismiss-button'
4
+
5
+ /** A notice's colour: blue, green, yellow or red, as WordPress's `notice-*` classes. */
6
+ export type NoticeType = 'info' | 'success' | 'warning' | 'error'
7
+
8
+ /**
9
+ * A notice, as printed on `admin_notices`. Text children are wrapped in a paragraph; pass
10
+ * elements to lay out more.
11
+ */
12
+ export function Notice({
13
+ type = 'info',
14
+ title,
15
+ role,
16
+ inline = false,
17
+ dismissible = false,
18
+ children,
19
+ }: {
20
+ type?: NoticeType
21
+ title?: ReactNode
22
+ /** `status` or `alert` when the notice reports what just happened. */
23
+ role?: 'status' | 'alert'
24
+ /** Inside a box or form rather than at the top of the screen. */
25
+ inline?: boolean
26
+ /** Adds the × that hides it (`is-dismissible`). */
27
+ dismissible?: boolean
28
+ children?: ReactNode
29
+ }) {
30
+ return (
31
+ <div
32
+ className={cx(
33
+ 'notice',
34
+ `notice-${type}`,
35
+ inline && 'inline',
36
+ dismissible && 'is-dismissible',
37
+ )}
38
+ role={role}
39
+ >
40
+ {title && (
41
+ <p>
42
+ <strong>{title}</strong>
43
+ </p>
44
+ )}
45
+ {typeof children === 'string' ? <p>{children}</p> : children}
46
+ {dismissible && <DismissButton />}
47
+ </div>
48
+ )
49
+ }
50
+
51
+ /** A meta box: a titled white box, as on the dashboard and edit screens (`add_meta_box`). */
52
+ export function Postbox({
53
+ id,
54
+ title,
55
+ actions,
56
+ children,
57
+ }: {
58
+ /** Unique on the screen; labels the box for assistive technology. */
59
+ id: string
60
+ title: ReactNode
61
+ /** Small controls on the right of the title bar. */
62
+ actions?: ReactNode
63
+ children: ReactNode
64
+ }) {
65
+ const heading = `${id}-title`
66
+ return (
67
+ <section className="postbox" id={id} aria-labelledby={heading}>
68
+ <div className="postbox-header">
69
+ <h2 className="hndle" id={heading}>
70
+ {title}
71
+ </h2>
72
+ {actions && <div className="postbox-actions">{actions}</div>}
73
+ </div>
74
+ <div className="inside">{children}</div>
75
+ </section>
76
+ )
77
+ }
78
+
79
+ /**
80
+ * An edit screen's two columns, as `#poststuff` lays them out: the main boxes, and a 280px
81
+ * sidebar for status and actions. Below 851px they stack, main first.
82
+ */
83
+ export function EditColumns({ main, side }: { main: ReactNode; side: ReactNode }) {
84
+ return (
85
+ <div className="edit-columns">
86
+ <div className="edit-columns-main">{main}</div>
87
+ <div className="edit-columns-side">{side}</div>
88
+ </div>
89
+ )
90
+ }
package/src/button.tsx ADDED
@@ -0,0 +1,76 @@
1
+ import type { AnchorHTMLAttributes, ButtonHTMLAttributes, ReactNode } from 'react'
2
+
3
+ /** WordPress's button styles: `button-primary`, `button` and the link-styled `button-link`. */
4
+ export type ButtonVariant = 'primary' | 'secondary' | 'link' | 'delete'
5
+
6
+ /** Joins class names, skipping the falsy ones: `cx('button', small && 'button-small')`. */
7
+ export function cx(...names: (string | false | null | undefined)[]): string {
8
+ return names.filter(Boolean).join(' ')
9
+ }
10
+
11
+ /** The class names for a button variant, for links and buttons styled by hand. */
12
+ export function buttonClass(variant: ButtonVariant = 'secondary', small = false): string {
13
+ return cx(
14
+ variant === 'primary' && 'button button-primary',
15
+ variant === 'secondary' && 'button',
16
+ variant === 'link' && 'button-link',
17
+ variant === 'delete' && 'button-link button-link-delete',
18
+ small && 'button-small',
19
+ )
20
+ }
21
+
22
+ interface Styled {
23
+ variant?: ButtonVariant
24
+ small?: boolean
25
+ }
26
+
27
+ /** A `<button>` in WordPress's styles. Defaults to `type="button"`, so it never submits by accident. */
28
+ export function Button({
29
+ variant,
30
+ small,
31
+ className,
32
+ type = 'button',
33
+ ...rest
34
+ }: Styled & ButtonHTMLAttributes<HTMLButtonElement>) {
35
+ return <button type={type} className={cx(buttonClass(variant, small), className)} {...rest} />
36
+ }
37
+
38
+ /** A link that looks like a button, such as "Add New". */
39
+ export function ButtonLink({
40
+ variant,
41
+ small,
42
+ className,
43
+ ...rest
44
+ }: Styled & AnchorHTMLAttributes<HTMLAnchorElement>) {
45
+ return <a className={cx(buttonClass(variant, small), className)} {...rest} />
46
+ }
47
+
48
+ /**
49
+ * A button that posts to an admin action, for row actions and screen actions that change data.
50
+ * Admin actions only take POST, so these are forms rather than links (WordPress uses nonced GETs).
51
+ */
52
+ export function FormButton({
53
+ action,
54
+ fields = {},
55
+ variant,
56
+ small,
57
+ className,
58
+ children,
59
+ ...rest
60
+ }: {
61
+ action: string
62
+ fields?: Readonly<Record<string, string>>
63
+ children: ReactNode
64
+ } & Styled &
65
+ Omit<ButtonHTMLAttributes<HTMLButtonElement>, 'type' | 'formAction'>) {
66
+ return (
67
+ <form method="post" action={action} className={cx('inline-form', className)}>
68
+ {Object.entries(fields).map(([name, value]) => (
69
+ <input key={name} type="hidden" name={name} value={value} />
70
+ ))}
71
+ <Button type="submit" variant={variant} small={small} {...rest}>
72
+ {children}
73
+ </Button>
74
+ </form>
75
+ )
76
+ }
@@ -0,0 +1,17 @@
1
+ 'use client'
2
+
3
+ /** The × on an `is-dismissible` notice. Hides it for this page view; nothing is stored. */
4
+ export function DismissButton() {
5
+ return (
6
+ <button
7
+ type="button"
8
+ className="notice-dismiss"
9
+ onClick={(event) => {
10
+ const notice = event.currentTarget.closest('.notice')
11
+ if (notice instanceof HTMLElement) notice.hidden = true
12
+ }}
13
+ >
14
+ <span className="screen-reader-text">Dismiss this notice.</span>
15
+ </button>
16
+ )
17
+ }
package/src/forms.tsx ADDED
@@ -0,0 +1,58 @@
1
+ import type { ButtonHTMLAttributes, ReactNode } from 'react'
2
+ import { Button } from './button'
3
+
4
+ /** Label on the left, control on the right; one row per field below 783px (`.form-table`). */
5
+ export function FormTable({ children }: { children: ReactNode }) {
6
+ return (
7
+ <table className="form-table" role="presentation">
8
+ <tbody>{children}</tbody>
9
+ </table>
10
+ )
11
+ }
12
+
13
+ /**
14
+ * One row of a form table. Pass `htmlFor` to label a single control; leave it out for a group,
15
+ * such as checkboxes in a fieldset, which labels itself.
16
+ */
17
+ export function FormField({
18
+ label,
19
+ htmlFor,
20
+ description,
21
+ descriptionId,
22
+ children,
23
+ }: {
24
+ label: ReactNode
25
+ htmlFor?: string
26
+ description?: ReactNode
27
+ /** For the control's `aria-describedby`. */
28
+ descriptionId?: string
29
+ children: ReactNode
30
+ }) {
31
+ return (
32
+ <tr>
33
+ <th scope="row">{htmlFor ? <label htmlFor={htmlFor}>{label}</label> : label}</th>
34
+ <td>
35
+ {children}
36
+ {description && (
37
+ <p className="description" id={descriptionId}>
38
+ {description}
39
+ </p>
40
+ )}
41
+ </td>
42
+ </tr>
43
+ )
44
+ }
45
+
46
+ /** The primary button under a form (`submit_button()`). */
47
+ export function SubmitButton({
48
+ children = 'Save Changes',
49
+ ...rest
50
+ }: { children?: ReactNode } & Omit<ButtonHTMLAttributes<HTMLButtonElement>, 'type'>) {
51
+ return (
52
+ <p className="submit">
53
+ <Button type="submit" variant="primary" {...rest}>
54
+ {children}
55
+ </Button>
56
+ </p>
57
+ )
58
+ }
@@ -0,0 +1,53 @@
1
+ import type { AdminIconName } from '@otwdesign/sdk'
2
+
3
+ /**
4
+ * Drawn for this project on a 20 × 20 grid, like Dashicons. `holes` icons fill even-odd, so a
5
+ * shape inside another cuts it out; the rest union their shapes.
6
+ */
7
+ export const ICONS: Record<AdminIconName, { d: string; holes?: true }> = {
8
+ admin: {
9
+ d: 'M16.38 7.66 L18.82 8.2 L18.82 11.8 L16.38 12.34 L16.17 12.86 L17.51 14.96 L14.96 17.51 L12.86 16.17 L12.34 16.38 L11.8 18.82 L8.2 18.82 L7.66 16.38 L7.14 16.17 L5.04 17.51 L2.49 14.96 L3.83 12.86 L3.62 12.34 L1.18 11.8 L1.18 8.2 L3.62 7.66 L3.83 7.14 L2.49 5.04 L5.04 2.49 L7.14 3.83 L7.66 3.62 L8.2 1.18 L11.8 1.18 L12.34 3.62 L12.86 3.83 L14.96 2.49 L17.51 5.04 L16.17 7.14ZM7.2 10a2.8 2.8 0 1 0 5.6 0a2.8 2.8 0 1 0 -5.6 0Z',
10
+ holes: true,
11
+ },
12
+ dashboard: {
13
+ d: 'M1.5 13A8.5 8.5 0 0 1 18.5 13L15.5 13A5.5 5.5 0 0 0 4.5 13ZM13.56 7.24 L14.94 8.4 L10.44 13.76 L9.06 12.6ZM8 13a2 2 0 1 0 4 0a2 2 0 1 0 -4 0Z',
14
+ },
15
+ // A pushpin, like Dashicons' admin-post.
16
+ post: {
17
+ d: 'M6 1.5H14V3.5H12.7L13.5 8.9C15.3 9.8 16.5 11.2 16.5 13H3.5C3.5 11.2 4.7 9.8 6.5 8.9L7.3 3.5H6ZM9.2 13H10.8V19H9.2Z',
18
+ },
19
+ plugins: { d: 'M6 1.5h2v5h-2ZM12 1.5h2v5h-2ZM4 7h12v3.5a5 5 0 0 1-5 5V19H9v-3.5a5 5 0 0 1-5-5Z' },
20
+ users: {
21
+ d: 'M6.4 6a3.6 3.6 0 1 0 7.2 0a3.6 3.6 0 1 0 -7.2 0ZM2.5 18.5c0-4.3 3.4-6.8 7.5-6.8s7.5 2.5 7.5 6.8Z',
22
+ },
23
+ settings: {
24
+ d: 'M2 4h2.4v1.6h-2.4ZM8.6 4h9.4v1.6h-9.4ZM5 2.3h3v5h-3ZM2 9.2h9.4v1.6h-9.4ZM15.6 9.2h2.4v1.6h-2.4ZM12 7.5h3v5h-3ZM2 14.4h4.4v1.6h-4.4ZM10.6 14.4h7.4v1.6h-7.4ZM7 12.7h3v5h-3Z',
25
+ },
26
+ cart: {
27
+ d: 'M1 2.5h3.2l.6 2.5H19l-2.2 7.5H6.9l.4 1.5H17v1.6H6l-3-11.5H1ZM6 17.5a1.5 1.5 0 1 0 3 0a1.5 1.5 0 1 0 -3 0ZM13.5 17.5a1.5 1.5 0 1 0 3 0a1.5 1.5 0 1 0 -3 0Z',
28
+ },
29
+ products: { d: 'M1.5 3h17v4.2h-17ZM2.8 8.4h14.4V18H2.8ZM7.5 10.2h5v1.6h-5Z', holes: true },
30
+ payments: { d: 'M1.5 4.5h17v11h-17ZM1.5 7h17v2.2h-17ZM3.5 11.5h5v1.6h-5Z', holes: true },
31
+ chart: { d: 'M2.5 11h3.6v7h-3.6ZM8.2 6.5h3.6v11.5h-3.6ZM13.9 2h3.6v16h-3.6Z' },
32
+ book: {
33
+ d: 'M10 4.8C8 3.3 5 2.8 1.5 3.4v12.8c3.4-.5 6.4 0 8.5 1.5 2.1-1.5 5.1-2 8.5-1.5V3.4C15 2.8 12 3.3 10 4.8ZM9.3 5.4h1.4v11.2h-1.4Z',
34
+ holes: true,
35
+ },
36
+ groups: {
37
+ d: 'M2.6 6.5a2.4 2.4 0 1 0 4.8 0a2.4 2.4 0 1 0 -4.8 0ZM12.6 6.5a2.4 2.4 0 1 0 4.8 0a2.4 2.4 0 1 0 -4.8 0ZM.5 15c0-3 2-4.8 4.5-4.8 1.1 0 2 .3 2.8.9-1 1.1-1.6 2.5-1.7 3.9ZM19.5 15c0-3-2-4.8-4.5-4.8-1.1 0-2 .3-2.8.9 1 1.1 1.6 2.5 1.7 3.9ZM7 7.5a3 3 0 1 0 6 0a3 3 0 1 0 -6 0ZM4.8 18.5c0-3.4 2.3-5.6 5.2-5.6s5.2 2.2 5.2 5.6Z',
38
+ },
39
+ media: {
40
+ d: 'M1.5 3.5h17v13h-17ZM3.2 5.2h13.6v9.6H3.2ZM4.5 13.5l3.6-4.8 2.6 3.2 1.8-2 3 3.6ZM12.1 7.5a1.4 1.4 0 1 0 2.8 0a1.4 1.4 0 1 0 -2.8 0Z',
41
+ holes: true,
42
+ },
43
+ page: {
44
+ d: 'M4 1.5h7.5l4.5 4.5v12.5H4ZM11 2.6V6.5h3.9ZM6.2 9h7.6v1.4h-7.6ZM6.2 12h7.6v1.4h-7.6ZM6.2 15h5v1.4h-5Z',
45
+ holes: true,
46
+ },
47
+ tools: { d: 'M3 2.5h14v4.8h-14ZM8.7 7.3h2.6v11.2h-2.6Z' },
48
+ megaphone: { d: 'M2 7.5h3.5L14 3v14l-8.5-4.5H2ZM6 13l1.6 5.5h2.2L8.4 13ZM15.5 8.6h3v2.8h-3Z' },
49
+ calendar: {
50
+ d: 'M2.5 4h15v14.5h-15ZM4 8.5h12v8.5H4ZM5.5 1.5h2v2.5h-2ZM12.5 1.5h2v2.5h-2ZM6 10.5h3v2.5h-3ZM11 10.5h3v2.5h-3Z',
51
+ holes: true,
52
+ },
53
+ }
package/src/icon.tsx ADDED
@@ -0,0 +1,34 @@
1
+ import type { AdminIcon as AdminIconValue } from '@otwdesign/sdk'
2
+ import { ICONS } from './icon-paths'
3
+
4
+ /**
5
+ * A menu or screen icon in the current text colour. Data URIs are drawn as a mask over that
6
+ * colour, so a plugin's SVG matches the menu around it, as WordPress tints them.
7
+ */
8
+ export function AdminIcon({ icon, className }: { icon: AdminIconValue; className?: string }) {
9
+ if (icon.startsWith('data:')) {
10
+ // isAdminIcon() allows only base64 data URIs, which cannot close the CSS string.
11
+ const mask = `url("${icon}") center / contain no-repeat`
12
+ return (
13
+ <span
14
+ className={className}
15
+ aria-hidden="true"
16
+ style={{ display: 'inline-block', background: 'currentColor', mask, WebkitMask: mask }}
17
+ />
18
+ )
19
+ }
20
+ const { d, holes } = ICONS[icon as keyof typeof ICONS] ?? ICONS.admin
21
+ return (
22
+ <svg
23
+ className={className}
24
+ viewBox="0 0 20 20"
25
+ width="20"
26
+ height="20"
27
+ fill="currentColor"
28
+ aria-hidden="true"
29
+ focusable="false"
30
+ >
31
+ <path d={d} fillRule={holes ? 'evenodd' : undefined} />
32
+ </svg>
33
+ )
34
+ }
package/src/index.ts ADDED
@@ -0,0 +1,12 @@
1
+ /*
2
+ * The admin's building blocks, rendered with WordPress's class names (`button-primary`,
3
+ * `notice-error`, `form-table`, `wp-list-table`, `postbox`) and styled by the admin. Plugins can
4
+ * use these components, or write the same markup by hand as WordPress plugins do.
5
+ */
6
+ export { Button, ButtonLink, FormButton, buttonClass, cx, type ButtonVariant } from './button'
7
+ export { EditColumns, Notice, Postbox, type NoticeType } from './boxes'
8
+ export { FormField, FormTable, SubmitButton } from './forms'
9
+ export { AdminIcon } from './icon'
10
+ export { ListTable, RowActions, TableNav, Views, type Column, type View } from './list-table'
11
+ export { DismissButton } from './dismiss-button'
12
+ export { RowToggle } from './row-toggle'
@@ -0,0 +1,258 @@
1
+ import type { ReactNode } from 'react'
2
+ import { cx } from './button'
3
+ import { RowToggle } from './row-toggle'
4
+ import { SelectAll } from './select-all'
5
+
6
+ /** A column of a `ListTable`: its header, and how a row renders in it. */
7
+ export interface Column<T> {
8
+ key: string
9
+ /** The header, and the label before the value when a phone expands the row. */
10
+ label: string
11
+ /**
12
+ * Stays visible on phones and carries the row actions. Defaults to the first column; WordPress
13
+ * calls it `column-primary`.
14
+ */
15
+ primary?: boolean
16
+ /** Right-aligned, with tabular figures: prices, totals, counts. */
17
+ numeric?: boolean
18
+ /**
19
+ * Makes the header a link that sorts by this column (`sortable`): `url` sorts it, and `current`
20
+ * says how the list is sorted by it now, if it is.
21
+ */
22
+ sort?: { url: string; current?: 'asc' | 'desc' }
23
+ render(row: T): ReactNode
24
+ }
25
+
26
+ /**
27
+ * WordPress's list table (`WP_List_Table`): striped rows, actions under the primary column that
28
+ * appear on hover or focus, and on phones a single column with a toggle for the rest.
29
+ */
30
+ export function ListTable<T>({
31
+ caption,
32
+ columns,
33
+ rows,
34
+ rowKey,
35
+ rowActions,
36
+ rowClassName,
37
+ select,
38
+ striped = true,
39
+ className,
40
+ empty,
41
+ }: {
42
+ /** Read to screen readers before the table. */
43
+ caption: string
44
+ columns: readonly Column<T>[]
45
+ rows: readonly T[]
46
+ rowKey(row: T): string
47
+ /** Links or buttons; separated by bars as in WordPress. */
48
+ rowActions?(row: T): ReactNode[]
49
+ /** `active` or `inactive` highlight a row, as on the plugins screen. */
50
+ rowClassName?(row: T): string | undefined
51
+ /**
52
+ * A checkbox per row for bulk actions, named `name` and holding `value(row)`, on the rows
53
+ * `enabled` allows. Put the table and the bulk controls in one form.
54
+ */
55
+ select?: {
56
+ name: string
57
+ value(row: T): string
58
+ label(row: T): string
59
+ enabled?(row: T): boolean
60
+ }
61
+ /** Alternate row shading. The plugins screen turns it off to show active rows instead. */
62
+ striped?: boolean
63
+ /** Extra classes on the table, such as `plugins`. */
64
+ className?: string
65
+ empty: ReactNode
66
+ }) {
67
+ const primary = columns.find((column) => column.primary) ?? columns[0]
68
+ const columnClass = (column: Column<T>) =>
69
+ cx(`column-${column.key}`, column === primary && 'column-primary', column.numeric && 'num')
70
+ const headerClass = (column: Column<T>) =>
71
+ cx(
72
+ columnClass(column),
73
+ column.sort && (column.sort.current ? `sorted ${column.sort.current}` : 'sortable'),
74
+ )
75
+
76
+ return (
77
+ <table className={cx('wp-list-table widefat', striped && 'striped', className)}>
78
+ <caption className="screen-reader-text">{caption}</caption>
79
+ <thead>
80
+ <tr>
81
+ {select && (
82
+ <td className="check-column">
83
+ <SelectAll name={select.name} />
84
+ </td>
85
+ )}
86
+ {columns.map((column) => (
87
+ <th
88
+ key={column.key}
89
+ scope="col"
90
+ className={headerClass(column)}
91
+ aria-sort={
92
+ column.sort?.current === 'asc'
93
+ ? 'ascending'
94
+ : column.sort?.current === 'desc'
95
+ ? 'descending'
96
+ : undefined
97
+ }
98
+ >
99
+ {column.sort ? (
100
+ <a href={column.sort.url}>
101
+ <span>{column.label}</span>
102
+ <span className="sorting-indicators" aria-hidden="true">
103
+ <span className="sorting-indicator asc" />
104
+ <span className="sorting-indicator desc" />
105
+ </span>
106
+ </a>
107
+ ) : (
108
+ column.label
109
+ )}
110
+ </th>
111
+ ))}
112
+ </tr>
113
+ </thead>
114
+ <tbody>
115
+ {rows.length === 0 ? (
116
+ <tr className="no-items">
117
+ <td colSpan={columns.length + (select ? 1 : 0)}>{empty}</td>
118
+ </tr>
119
+ ) : (
120
+ rows.map((row) => {
121
+ const actions = rowActions?.(row) ?? []
122
+ return (
123
+ <tr key={rowKey(row)} className={rowClassName?.(row)}>
124
+ {select && (
125
+ <th scope="row" className="check-column">
126
+ {(select.enabled?.(row) ?? true) && (
127
+ <input
128
+ type="checkbox"
129
+ name={select.name}
130
+ value={select.value(row)}
131
+ aria-label={`Select ${select.label(row)}`}
132
+ />
133
+ )}
134
+ </th>
135
+ )}
136
+ {columns.map((column) => (
137
+ <td key={column.key} className={columnClass(column)} data-colname={column.label}>
138
+ {column.render(row)}
139
+ {column === primary && (
140
+ <>
141
+ {actions.length > 0 && <RowActions actions={actions} />}
142
+ {columns.length > 1 && <RowToggle />}
143
+ </>
144
+ )}
145
+ </td>
146
+ ))}
147
+ </tr>
148
+ )
149
+ })
150
+ )}
151
+ </tbody>
152
+ </table>
153
+ )
154
+ }
155
+
156
+ /** Links under a row's primary column, separated by bars: Edit | View | Trash. */
157
+ export function RowActions({ actions }: { actions: readonly ReactNode[] }) {
158
+ return (
159
+ <div className="row-actions">
160
+ {actions.map((action, index) => (
161
+ <span key={index}>
162
+ {action}
163
+ {index < actions.length - 1 && ' | '}
164
+ </span>
165
+ ))}
166
+ </div>
167
+ )
168
+ }
169
+
170
+ /** One of the filter links above a list, with its count. */
171
+ export interface View {
172
+ label: string
173
+ href: string
174
+ count?: number
175
+ current?: boolean
176
+ }
177
+
178
+ /** The filter links above a list: All (12) | Active (3) | … (`.subsubsub`). */
179
+ export function Views({ label, views }: { label: string; views: readonly View[] }) {
180
+ return (
181
+ <nav aria-label={label} className="views">
182
+ <ul className="subsubsub">
183
+ {views.map((view, index) => (
184
+ <li key={view.href}>
185
+ <a
186
+ href={view.href}
187
+ className={view.current ? 'current' : undefined}
188
+ aria-current={view.current ? 'page' : undefined}
189
+ >
190
+ {view.label}
191
+ {view.count !== undefined && (
192
+ <>
193
+ {' '}
194
+ <span className="count">({view.count})</span>
195
+ </>
196
+ )}
197
+ </a>
198
+ {index < views.length - 1 && ' |'}
199
+ </li>
200
+ ))}
201
+ </ul>
202
+ </nav>
203
+ )
204
+ }
205
+
206
+ /**
207
+ * The bar above or below a list: bulk controls on the left, the count and page links on the
208
+ * right (`.tablenav`).
209
+ */
210
+ export function TableNav({
211
+ total,
212
+ noun,
213
+ page = 1,
214
+ pages = 1,
215
+ pageUrl,
216
+ children,
217
+ }: {
218
+ total: number
219
+ /** Singular and plural, as in `['item', 'items']`. */
220
+ noun: readonly [string, string]
221
+ page?: number
222
+ pages?: number
223
+ pageUrl?(page: number): string
224
+ children?: ReactNode
225
+ }) {
226
+ const link = (target: number, className: string, label: string, symbol: string) =>
227
+ target === page || target < 1 || target > pages || !pageUrl ? (
228
+ <span className="tablenav-pages-navspan button disabled" aria-hidden="true">
229
+ {symbol}
230
+ </span>
231
+ ) : (
232
+ <a className={cx(className, 'button')} href={pageUrl(target)}>
233
+ <span className="screen-reader-text">{label}</span>
234
+ <span aria-hidden="true">{symbol}</span>
235
+ </a>
236
+ )
237
+ return (
238
+ <div className="tablenav">
239
+ {children && <div className="alignleft actions">{children}</div>}
240
+ <div className={cx('tablenav-pages', pages <= 1 && 'one-page')}>
241
+ <span className="displaying-num">
242
+ {total} {total === 1 ? noun[0] : noun[1]}
243
+ </span>
244
+ {pages > 1 && (
245
+ <span className="pagination-links">
246
+ {link(1, 'first-page', 'First page', '«')}
247
+ {link(page - 1, 'prev-page', 'Previous page', '‹')}
248
+ <span className="paging-input">
249
+ {page} of <span className="total-pages">{pages}</span>
250
+ </span>
251
+ {link(page + 1, 'next-page', 'Next page', '›')}
252
+ {link(pages, 'last-page', 'Last page', '»')}
253
+ </span>
254
+ )}
255
+ </div>
256
+ </div>
257
+ )
258
+ }
@@ -0,0 +1,26 @@
1
+ 'use client'
2
+
3
+ import { useState } from 'react'
4
+
5
+ /**
6
+ * On phones a list table shows only its primary column; this shows the rest of the row, as
7
+ * WordPress's "Show more details" button does. The class drives the CSS, so it sits on the row.
8
+ */
9
+ export function RowToggle() {
10
+ const [expanded, setExpanded] = useState(false)
11
+ return (
12
+ <button
13
+ type="button"
14
+ className="toggle-row"
15
+ aria-expanded={expanded}
16
+ onClick={(event) => {
17
+ event.currentTarget.closest('tr')?.classList.toggle('is-expanded', !expanded)
18
+ setExpanded(!expanded)
19
+ }}
20
+ >
21
+ <span className="screen-reader-text">
22
+ {expanded ? 'Show fewer details' : 'Show more details'}
23
+ </span>
24
+ </button>
25
+ )
26
+ }
@@ -0,0 +1,18 @@
1
+ 'use client'
2
+
3
+ /** The checkbox in a list table's header that ticks every row's box (`#cb-select-all`). */
4
+ export function SelectAll({ name }: { name: string }) {
5
+ return (
6
+ <input
7
+ type="checkbox"
8
+ aria-label="Select all"
9
+ onChange={(event) => {
10
+ const table = event.currentTarget.closest('table')
11
+ for (const box of table?.querySelectorAll<HTMLInputElement>('input[type="checkbox"]') ??
12
+ []) {
13
+ if (box.name === name) box.checked = event.currentTarget.checked
14
+ }
15
+ }}
16
+ />
17
+ )
18
+ }