@the-portland-company/shell 0.87.1 → 0.88.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/CHANGELOG.md CHANGED
@@ -1,3 +1,14 @@
1
+ ## 0.88.0
2
+
3
+ Shared form primitives, so apps stop hand-rolling them.
4
+
5
+ - **New exports** `Button`, `Input`, `Select`, `Textarea` (plus `ButtonProps`, `ButtonVariant`, `ButtonSize`, `InputProps`, `SelectProps`, `TextareaProps`).
6
+ - **Button.** Variants `primary | secondary | ghost | danger`; sizes `md` (44px) and `sm` (36px); `loading` and `disabled`; `href` / `as` link-buttons; forwards its ref. Primary uses the `--app-btn-primary-*` tokens (Specs buttonPrimary).
7
+ - **Input, Select, Textarea.** `label`, `helperText`, `error`, `invalid`; 44px control height for Input and Select; forward their refs; `--app-field-*` tokens.
8
+ - **Rule.** Apps use these instead of a local Button / Input / Select / Textarea (README "Form primitives").
9
+ - **Native subpath.** `Button`, `Input`, `Select`, `Textarea`, `Card`, `PageHeader`, `FormColumn`, `BreadcrumbBar`, `HeaderTabBar` (and their types) are also exported from `@the-portland-company/shell/native`, which has no Chakra or react-router peer. The native `PageHeader` and `BreadcrumbBar` render breadcrumb links as plain anchors. The main entry is unchanged.
10
+ - Tests: `__tests__/formPrimitives.test.tsx`.
11
+
1
12
  ## 0.87.1
2
13
 
3
14
  Release fix only; same code as 0.87.0, which never reached npm. The 0.87.0 version bump regenerated `package-lock.json` with a newer npm that dropped two optional `yaml@2.9.1` entries, so the publish gate's `npm ci` refused it. The lock file is restored from 0.86.0 with only its version changed.
package/README.md CHANGED
@@ -171,6 +171,52 @@ All `<ShellChrome>` props are optional — only supply what your app has. The ch
171
171
 
172
172
  ---
173
173
 
174
+ ## Form primitives
175
+
176
+ Shared `Button`, `Input`, `Select` and `Textarea`, styled from the shell's tokens (`--app-btn-*`, `--app-field-*`, `--color-*`). 5px radius, 12px horizontal padding.
177
+
178
+ | Export | Props |
179
+ |---|---|
180
+ | `<Button>` | `variant`: `primary` (default) \| `secondary` \| `ghost` \| `danger`; `size`: `md` (44px, default) \| `sm` (36px); `loading` (spinner, `aria-busy`, blocks clicks); `disabled`; `href` or `as` for a link-button (e.g. `as={Link} to="/x"`); `type` defaults to `button`; forwards its ref. |
181
+ | `<Input>` | All native input props plus `label`, `helperText`, `error`, `invalid`, `wrapperClassName`. 44px tall. Forwards its ref to the `<input>`. |
182
+ | `<Select>` | Same as `Input`, with `<option>` children. 44px tall. Forwards its ref to the `<select>`. |
183
+ | `<Textarea>` | Same as `Input`, 88px minimum height. Forwards its ref to the `<textarea>`. |
184
+
185
+ An `error` message marks the control invalid (`aria-invalid`, red border) and replaces the helper text; the label, helper and error are wired with `htmlFor` and `aria-describedby`.
186
+
187
+ These primitives (and `Card`, `PageHeader`, `FormColumn`) are also exported from `@the-portland-company/shell/native`, which needs no Chakra or react-router peer. Apps without those peers should import from `native`.
188
+
189
+ **Rule:** apps must use these instead of a local `Button`, `Input`, `Select` or `Textarea`. Do not hand-roll form controls or button styles in an app; if a control needs a new variant or state, add it here so every app gets it.
190
+
191
+ ## Enforcement
192
+
193
+ Spec: Components — shared primitives are mandatory. Apps must not hand-roll Button/Card/Input/Select/Textarea, and must not fall behind the shell version. Both are checked mechanically.
194
+
195
+ Adopt in three lines:
196
+
197
+ ```js
198
+ // eslint.config.js (ESLint 9+ flat config)
199
+ import shell from '@the-portland-company/shell/eslint'
200
+ export default [...shell.configs.recommended /* , your other configs */]
201
+ ```
202
+
203
+ ```json
204
+ // package.json
205
+ { "scripts": { "lint": "eslint . && shell-version-check" } }
206
+ ```
207
+
208
+ Run `shell-version-check` in CI too (it is a package `bin`, so `npx shell-version-check` works after install).
209
+
210
+ | Rule (`politogy-shell/…`) | Fails on |
211
+ |---|---|
212
+ | `no-local-primitives` | Declaring or exporting a local `Button`, `Card`, `Input`, `Select`, `Textarea`, `PageHeader` or `FormColumn` (re-exports from the shell are fine). |
213
+ | `no-raw-control-styling` | JSX `<button>`, `<input>`, `<select>`, `<textarea>` with `className` or `style`. Allowed in a file that imports the shell primitive of the same kind (wrapping). |
214
+ | `no-global-control-classes` | `className` strings matching `\b(btn\|input)(-[a-z]+)?\b`, including inside `clsx(...)`, ternaries and template literals. |
215
+
216
+ Escape hatch: put `// shell-primitive-exempt: <reason>` on the line above (or the same line as) the offending code. A reason is required. Prefer adding the missing variant to the shell instead.
217
+
218
+ `shell-version-check` exits 1 when the app's declared shell range cannot install the latest published version (`npm view`, 10s timeout; exit 2 if that lookup fails). `--offline` compares against the installed shell version instead; `--cwd=<dir>` checks another package. The shell package itself does not enable these rules.
219
+
174
220
  ## CSS imports
175
221
 
176
222
  ```tsx
@@ -0,0 +1,117 @@
1
+ #!/usr/bin/env node
2
+ // Fails when the app's declared @the-portland-company/shell range cannot
3
+ // satisfy the latest published minor (or, with --offline, the installed one).
4
+ // Exit: 0 ok, 1 behind, 2 could not determine.
5
+ import { readFileSync, existsSync, realpathSync } from 'node:fs'
6
+ import { execFileSync } from 'node:child_process'
7
+ import { resolve, join } from 'node:path'
8
+ import { pathToFileURL } from 'node:url'
9
+
10
+ export const PKG = '@the-portland-company/shell'
11
+
12
+ const V = /^v?(\d+)\.(\d+)\.(\d+)/
13
+ export function parseVersion(s) {
14
+ const m = V.exec(String(s).trim())
15
+ return m ? [Number(m[1]), Number(m[2]), Number(m[3])] : null
16
+ }
17
+ const cmp = (a, b) => a[0] - b[0] || a[1] - b[1] || a[2] - b[2]
18
+
19
+ // One comparator token -> predicate list. Supports ^ ~ >= > <= < = x-ranges and *.
20
+ function comparator(tok) {
21
+ tok = tok.trim()
22
+ if (tok === '' || tok === '*' || tok === 'x' || tok === 'latest') return [() => true]
23
+ const m = /^(\^|~|>=|<=|>|<|=)?\s*v?(\d+|x|\*)(?:\.(\d+|x|\*))?(?:\.(\d+|x|\*))?(?:-[\w.]+)?$/.exec(tok)
24
+ if (!m) throw new Error(`Unsupported range "${tok}"`)
25
+ const op = m[1] || ''
26
+ const wild = (x) => x === undefined || x === 'x' || x === '*'
27
+ const p = [m[2], m[3], m[4]].map((x) => (wild(x) ? null : Number(x)))
28
+ const lo = [p[0] ?? 0, p[1] ?? 0, p[2] ?? 0]
29
+ const ge = (v) => cmp(v, lo) >= 0
30
+ if (p[0] === null) return [() => true]
31
+ if (op === '>=') return [ge]
32
+ if (op === '>') return [(v) => (p[1] === null ? v[0] > p[0] : p[2] === null ? v[0] > p[0] || (v[0] === p[0] && v[1] > p[1]) : cmp(v, lo) > 0)]
33
+ if (op === '<') return [(v) => cmp(v, lo) < 0]
34
+ if (op === '<=') return [(v) => (p[1] === null ? v[0] <= p[0] : p[2] === null ? v[0] < p[0] || (v[0] === p[0] && v[1] <= p[1]) : cmp(v, lo) <= 0)]
35
+ let upper
36
+ if (op === '^') {
37
+ upper = p[0] > 0 || p[1] === null ? [p[0] + 1, 0, 0] : p[1] > 0 || p[2] === null ? [0, p[1] + 1, 0] : [0, 0, p[2] + 1]
38
+ } else if (op === '~') {
39
+ upper = p[1] === null ? [p[0] + 1, 0, 0] : [p[0], p[1] + 1, 0]
40
+ } else {
41
+ // exact or x-range
42
+ upper = p[1] === null ? [p[0] + 1, 0, 0] : p[2] === null ? [p[0], p[1] + 1, 0] : [p[0], p[1], p[2] + 1]
43
+ }
44
+ return [ge, (v) => cmp(v, upper) < 0]
45
+ }
46
+
47
+ export function satisfies(version, range) {
48
+ const v = parseVersion(version)
49
+ if (!v) throw new Error(`Bad version "${version}"`)
50
+ const r = String(range).trim()
51
+ if (/^(npm:|file:|link:|git|https?:|workspace:)/.test(r)) throw new Error(`Cannot evaluate range "${r}"`)
52
+ return r.split('||').some((set) => {
53
+ const toks = set.trim().replace(/(>=|<=|>|<|=|\^|~)\s+/g, '$1').split(/\s+/)
54
+ return toks.every((t) => comparator(t).every((f) => f(v)))
55
+ })
56
+ }
57
+
58
+ export function declaredRange(pkgJson) {
59
+ for (const k of ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies']) {
60
+ if (pkgJson[k] && pkgJson[k][PKG]) return pkgJson[k][PKG]
61
+ }
62
+ return null
63
+ }
64
+
65
+ /** Pure decision: returns { ok, message }. */
66
+ export function evaluate({ range, target, source }) {
67
+ if (satisfies(target, range)) {
68
+ return { ok: true, message: `ok: ${PKG} "${range}" accepts ${source} ${target}.` }
69
+ }
70
+ const [maj, min] = parseVersion(target)
71
+ return {
72
+ ok: false,
73
+ message:
74
+ `FAIL: ${PKG} is declared as "${range}", which cannot install ${source} ${target}.\n` +
75
+ `Upgrade: npm install ${PKG}@^${maj}.${min}.0 (apps must not fall behind the shell).`,
76
+ }
77
+ }
78
+
79
+ function main(argv) {
80
+ const offline = argv.includes('--offline')
81
+ const cwdArg = argv.find((a) => a.startsWith('--cwd='))
82
+ const cwd = resolve(cwdArg ? cwdArg.slice(6) : process.cwd())
83
+ const pj = join(cwd, 'package.json')
84
+ if (!existsSync(pj)) {
85
+ console.error(`shell-version-check: no package.json in ${cwd}`)
86
+ return 2
87
+ }
88
+ const range = declaredRange(JSON.parse(readFileSync(pj, 'utf8')))
89
+ if (!range) {
90
+ console.error(`shell-version-check: ${PKG} is not a dependency of this package.`)
91
+ return 2
92
+ }
93
+ let target, source
94
+ try {
95
+ if (offline) {
96
+ const ip = join(cwd, 'node_modules', ...PKG.split('/'), 'package.json')
97
+ if (!existsSync(ip)) throw new Error(`${PKG} is not installed (run npm install)`)
98
+ target = JSON.parse(readFileSync(ip, 'utf8')).version
99
+ source = 'installed'
100
+ } else {
101
+ const out = execFileSync('npm', ['view', PKG, 'version'], { encoding: 'utf8', timeout: 10_000, stdio: ['ignore', 'pipe', 'pipe'] })
102
+ target = out.trim()
103
+ source = 'latest published'
104
+ }
105
+ const res = evaluate({ range, target, source })
106
+ ;(res.ok ? console.log : console.error)(res.message)
107
+ return res.ok ? 0 : 1
108
+ } catch (e) {
109
+ console.error(`shell-version-check: could not determine version (${e.message.split('\n')[0]}). Use --offline to compare against the installed shell.`)
110
+ return 2
111
+ }
112
+ }
113
+
114
+ if (process.argv[1] && import.meta.url === pathToFileURL(realpathSync(process.argv[1])).href) {
115
+ process.exit(main(process.argv.slice(2)))
116
+ }
117
+ export { main }