@hubpav/hwio-ui 0.7.8 → 0.7.10
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/package.json +6 -5
- package/src/component-sources.mjs +125 -0
- package/src/components/HWioHeader.astro +10 -4
- package/src/integration.d.mts +6 -0
- package/src/integration.mjs +9 -5
- package/src/styles/styles.css +13 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hubpav/hwio-ui",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.10",
|
|
4
4
|
"description": "HARDWARIO web design system for Astro: DaisyUI 5 themes generated from the brand tokens, the Tailwind entry stylesheet and the HWio* components.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -28,12 +28,13 @@
|
|
|
28
28
|
"./html": "./src/html.ts"
|
|
29
29
|
},
|
|
30
30
|
"scripts": {
|
|
31
|
-
"build": "node scripts/build-themes.mjs"
|
|
31
|
+
"build": "node scripts/build-themes.mjs",
|
|
32
|
+
"test": "node --test \"test/**/*.test.mjs\""
|
|
32
33
|
},
|
|
33
34
|
"dependencies": {
|
|
34
|
-
"@hubpav/hwio-brand": "0.7.
|
|
35
|
-
"@hubpav/hwio-web-runtime": "0.7.
|
|
36
|
-
"@hubpav/hwio-web-worker": "0.7.
|
|
35
|
+
"@hubpav/hwio-brand": "0.7.10",
|
|
36
|
+
"@hubpav/hwio-web-runtime": "0.7.10",
|
|
37
|
+
"@hubpav/hwio-web-worker": "0.7.10",
|
|
37
38
|
"daisyui": "5.7.47"
|
|
38
39
|
},
|
|
39
40
|
"peerDependencies": {
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// Tailwind scans the package's components through `@source "../components"` in styles.css, so a
|
|
2
|
+
// page used to carry DaisyUI and utility rules for every HWio component, rendered or not. The
|
|
3
|
+
// Vite plugin below appends `@source not` lines for the components a site never imports to the
|
|
4
|
+
// site's entry stylesheet (the one that imports `@hubpav/hwio-ui/styles.css`). It keeps every
|
|
5
|
+
// component when it cannot be sure: no import found, or an import it cannot read literally.
|
|
6
|
+
import { existsSync, readdirSync, readFileSync, realpathSync, statSync } from 'node:fs';
|
|
7
|
+
import { dirname, join, relative, resolve, sep } from 'node:path';
|
|
8
|
+
import { fileURLToPath } from 'node:url';
|
|
9
|
+
|
|
10
|
+
export const HWIO_COMPONENTS_DIR = fileURLToPath(new URL('./components/', import.meta.url));
|
|
11
|
+
|
|
12
|
+
const ENTRY_IMPORT = /@import\s+(?:url\()?["']@hubpav\/hwio-ui\/styles\.css["']/;
|
|
13
|
+
const LITERAL_IMPORT = /@hubpav\/hwio-ui\/components\/(HWio[A-Za-z0-9]+)\.astro/g;
|
|
14
|
+
const ANY_IMPORT = /@hubpav\/hwio-ui\/components\//g;
|
|
15
|
+
const LOCAL_IMPORT = /from\s+["']\.\/(HWio[A-Za-z0-9]+)\.astro["']/g;
|
|
16
|
+
// Plain Markdown cannot import a component; MDX can.
|
|
17
|
+
const SCAN_EXT = /\.(astro|[cm]?[jt]sx?|mdx|svelte|vue)$/;
|
|
18
|
+
const SKIP_DIRS = new Set(['node_modules', 'dist', '.astro', '.git', '.wrangler', 'public']);
|
|
19
|
+
|
|
20
|
+
/** Component name -> the components it imports, read from the package's own sources. */
|
|
21
|
+
export function hwioComponentGraph(dir = HWIO_COMPONENTS_DIR) {
|
|
22
|
+
const graph = new Map();
|
|
23
|
+
for (const file of readdirSync(dir)) {
|
|
24
|
+
if (!file.endsWith('.astro')) continue;
|
|
25
|
+
const source = readFileSync(join(dir, file), 'utf8');
|
|
26
|
+
graph.set(file.slice(0, -'.astro'.length), new Set(Array.from(source.matchAll(LOCAL_IMPORT), (m) => m[1])));
|
|
27
|
+
}
|
|
28
|
+
return graph;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The components to leave out of the scan, given the text of every file that may import one.
|
|
33
|
+
* Returns null (scan everything) when nothing imports a component or an import is not a literal
|
|
34
|
+
* `@hubpav/hwio-ui/components/HWioName.astro` path (a computed path could name any of them).
|
|
35
|
+
*/
|
|
36
|
+
export function hwioUnusedComponents(texts, graph = hwioComponentGraph()) {
|
|
37
|
+
const used = new Set();
|
|
38
|
+
for (const text of texts) {
|
|
39
|
+
const literal = Array.from(text.matchAll(LITERAL_IMPORT), (m) => m[1]);
|
|
40
|
+
if (Array.from(text.matchAll(ANY_IMPORT)).length !== literal.length) return null;
|
|
41
|
+
literal.forEach((name) => used.add(name));
|
|
42
|
+
}
|
|
43
|
+
if (!used.size || [...used].some((name) => !graph.has(name))) return null;
|
|
44
|
+
const stack = [...used];
|
|
45
|
+
while (stack.length) {
|
|
46
|
+
for (const dep of graph.get(stack.pop()) ?? []) {
|
|
47
|
+
if (!used.has(dep)) { used.add(dep); stack.push(dep); }
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return [...graph.keys()].filter((name) => !used.has(name)).sort();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function walk(dir, out) {
|
|
54
|
+
let entries;
|
|
55
|
+
try { entries = readdirSync(dir, { withFileTypes: true }); } catch { return out; }
|
|
56
|
+
for (const entry of entries) {
|
|
57
|
+
if (SKIP_DIRS.has(entry.name)) continue;
|
|
58
|
+
const path = join(dir, entry.name);
|
|
59
|
+
if (entry.isDirectory()) walk(path, out);
|
|
60
|
+
else if (entry.isFile() && SCAN_EXT.test(entry.name)) out.push(path);
|
|
61
|
+
}
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Real directory of an installed dependency, looked up the way Node does (walking up). */
|
|
66
|
+
function packageDir(name, from) {
|
|
67
|
+
for (let dir = from; ; dir = dirname(dir)) {
|
|
68
|
+
const candidate = join(dir, 'node_modules', name);
|
|
69
|
+
if (existsSync(candidate)) return realpathSync(candidate);
|
|
70
|
+
if (dirname(dir) === dir) return null;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Where a site's component imports can live: the project, every local (workspace or `file:`)
|
|
76
|
+
* dependency, and every directory the entry stylesheet itself registers with `@source`.
|
|
77
|
+
*/
|
|
78
|
+
export function hwioScanRoots(root, cssFile, css) {
|
|
79
|
+
const roots = new Set([root]);
|
|
80
|
+
try {
|
|
81
|
+
const pkg = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8'));
|
|
82
|
+
for (const name of Object.keys({ ...pkg.dependencies, ...pkg.devDependencies })) {
|
|
83
|
+
if (name.startsWith('@hubpav/hwio-')) continue; // the design system itself (linked in its own workspace)
|
|
84
|
+
const dir = packageDir(name, root);
|
|
85
|
+
if (dir && !dir.split(sep).includes('node_modules')) roots.add(dir);
|
|
86
|
+
}
|
|
87
|
+
} catch { /* no package.json: the project alone */ }
|
|
88
|
+
for (const m of css.matchAll(/@source\s+(?!not\b)(?!inline\()["']([^"']+)["']/g)) {
|
|
89
|
+
const dir = resolve(dirname(cssFile), m[1]);
|
|
90
|
+
if (!dir.split(sep).includes('node_modules') && existsSync(dir)) roots.add(statSync(dir).isDirectory() ? dir : dirname(dir));
|
|
91
|
+
}
|
|
92
|
+
return [...roots];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Vite plugin: a `load` hook, so it works whatever order the Tailwind plugin runs in (Tailwind
|
|
97
|
+
* transforms the entry stylesheet after it is loaded and reads its @imports from disk itself).
|
|
98
|
+
* @param {{ root: string, logger?: { info: (msg: string) => void } }} options
|
|
99
|
+
*/
|
|
100
|
+
export function hwioComponentSourcesPlugin({ root, logger }) {
|
|
101
|
+
const graph = hwioComponentGraph();
|
|
102
|
+
const reported = new Set();
|
|
103
|
+
return {
|
|
104
|
+
name: '@hubpav/hwio-ui:component-sources',
|
|
105
|
+
enforce: 'pre',
|
|
106
|
+
load(id) {
|
|
107
|
+
const file = id.split('?')[0];
|
|
108
|
+
if (!file.endsWith('.css') || file.split(sep).includes('node_modules') || !existsSync(file)) return null;
|
|
109
|
+
const css = readFileSync(file, 'utf8');
|
|
110
|
+
if (!ENTRY_IMPORT.test(css)) return null;
|
|
111
|
+
const files = [...new Set(hwioScanRoots(root, file, css).flatMap((dir) => walk(dir, [])))];
|
|
112
|
+
for (const f of files) this.addWatchFile?.(f);
|
|
113
|
+
const unused = hwioUnusedComponents(files.map((f) => readFileSync(f, 'utf8')), graph);
|
|
114
|
+
if (!reported.has(file)) {
|
|
115
|
+
reported.add(file);
|
|
116
|
+
logger?.info(unused
|
|
117
|
+
? `Tailwind scans ${graph.size - unused.length} of ${graph.size} HWio components (${relative(root, file)})`
|
|
118
|
+
: `Tailwind scans all ${graph.size} HWio components (${relative(root, file)})`);
|
|
119
|
+
}
|
|
120
|
+
if (!unused?.length) return null;
|
|
121
|
+
const lines = unused.map((name) => `@source not "${relative(dirname(file), join(HWIO_COMPONENTS_DIR, `${name}.astro`)).split(sep).join('/')}";`);
|
|
122
|
+
return `${css}\n/* @hubpav/hwio-ui: components this site never imports */\n${lines.join('\n')}\n`;
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
}
|
|
@@ -8,6 +8,10 @@
|
|
|
8
8
|
* nav runtime keeps the checkbox, `aria-expanded` and focus in sync (into the panel on open, back to the
|
|
9
9
|
* trigger on close, Escape closes). Below `sm` the bar drops the language dropdown (the drawer carries the
|
|
10
10
|
* pills), so logo, theme toggle and the 48 px trigger fit a 320 px viewport without horizontal overflow.
|
|
11
|
+
*
|
|
12
|
+
* `navDensity="tight"` with `desktopFrom="xl"`: between 1280 and 1535 px, where the container is still
|
|
13
|
+
* 1280 px wide, the nav labels drop from 15 to 14 px and the item padding from 12 to 8 px, so a long nav
|
|
14
|
+
* plus CTAs (SK/PL/CS labels) fits without sideways scroll. From 1536 px the nav is back to default.
|
|
11
15
|
*/
|
|
12
16
|
import type { HWioCta, HWioLinkItem } from '../types';
|
|
13
17
|
import HWioButton from './HWioButton.astro';
|
|
@@ -30,13 +34,15 @@ interface Props {
|
|
|
30
34
|
labels: { nav: string; menu: string; close: string; theme?: string; languages?: string };
|
|
31
35
|
/** Viewport from which the inline nav and CTAs replace the drawer (default lg; xl for long navs). */
|
|
32
36
|
desktopFrom?: 'lg' | 'xl';
|
|
37
|
+
/** 'tight' (with desktopFrom xl only): smaller nav labels and padding from 1280 to 1535 px. Default 'default'. */
|
|
38
|
+
navDensity?: 'default' | 'tight';
|
|
33
39
|
id?: string;
|
|
34
40
|
class?: string;
|
|
35
41
|
}
|
|
36
|
-
const { nav, ctas = [], languages = [], showThemeToggle = true, scrollspy = false, labels, desktopFrom = 'lg', id = 'hwio-nav-drawer', class: className = '' } = Astro.props;
|
|
42
|
+
const { nav, ctas = [], languages = [], showThemeToggle = true, scrollspy = false, labels, desktopFrom = 'lg', navDensity = 'default', id = 'hwio-nav-drawer', class: className = '' } = Astro.props;
|
|
37
43
|
const BREAK = {
|
|
38
|
-
lg: { nav: 'hidden flex-1 self-stretch justify-center lg:flex', ctas: 'hidden items-center gap-2 lg:flex', drawer: 'drawer drawer-end w-auto lg:hidden', panels: 'hidden lg:block' },
|
|
39
|
-
xl: { nav: 'hidden flex-1 self-stretch justify-center xl:flex', ctas: 'hidden items-center gap-2 xl:flex', drawer: 'drawer drawer-end w-auto xl:hidden', panels: 'hidden xl:block' },
|
|
44
|
+
lg: { nav: 'hidden flex-1 self-stretch justify-center lg:flex', ctas: 'hidden items-center gap-2 lg:flex', drawer: 'drawer drawer-end w-auto lg:hidden', panels: 'hidden lg:block', densityTight: '' },
|
|
45
|
+
xl: { nav: 'hidden flex-1 self-stretch justify-center xl:flex', ctas: 'hidden items-center gap-2 xl:flex', drawer: 'drawer drawer-end w-auto xl:hidden', panels: 'hidden xl:block', densityTight: 'hwio-nav-tight' },
|
|
40
46
|
} as const;
|
|
41
47
|
const bp = BREAK[desktopFrom];
|
|
42
48
|
const CTA_VARIANT = { primary: 'primary', secondary: 'secondary', ghost: 'ghost' } as const;
|
|
@@ -50,7 +56,7 @@ const linkAttrs = (item: HWioLinkItem) => (item.external ? { target: '_blank', r
|
|
|
50
56
|
</div>
|
|
51
57
|
|
|
52
58
|
<nav class={bp.nav} aria-label={labels.nav} data-hwio-scrollspy={scrollspy ? 'true' : undefined}>
|
|
53
|
-
<ul class=
|
|
59
|
+
<ul class:list={['menu hwio-nav menu-horizontal h-full flex-nowrap items-stretch gap-0 px-0 py-0 whitespace-nowrap', navDensity === 'tight' && bp.densityTight]}>
|
|
54
60
|
{nav.map((item) => (
|
|
55
61
|
<li class:list={[item.current && 'hwio-nav-current']}>
|
|
56
62
|
{item.mega ? (
|
package/src/integration.d.mts
CHANGED
|
@@ -7,5 +7,11 @@ export interface HWioUiOptions {
|
|
|
7
7
|
assets?: boolean;
|
|
8
8
|
/** Font families copied into /fonts (file-name prefixes in @hubpav/hwio-brand/dist/fonts). Default ['inter']. */
|
|
9
9
|
fonts?: string[];
|
|
10
|
+
/**
|
|
11
|
+
* 'used' (default): Tailwind scans only the HWio components the site imports (found in the project,
|
|
12
|
+
* its local workspace dependencies and the entry stylesheet's own `@source` directories), so pages
|
|
13
|
+
* carry no rules for components they never render. 'all': scan every component.
|
|
14
|
+
*/
|
|
15
|
+
componentCss?: 'used' | 'all';
|
|
10
16
|
}
|
|
11
17
|
export function hwioUi(options?: HWioUiOptions): AstroIntegration;
|
package/src/integration.mjs
CHANGED
|
@@ -3,12 +3,14 @@
|
|
|
3
3
|
// integrations: [hwioUi({ headers: { gtm: true, turnstile: true, submitEndpoint: 'https://submit.hardwario.com', earlyHints: ['/fonts/inter-latin.woff2', '/fonts/inter-latin-ext.woff2'] } })]
|
|
4
4
|
// It (1) forces component scripts to stay external (script-src 'self' covers them), (2) serves the
|
|
5
5
|
// brand fonts and logos in dev and copies them into the build output, (3) writes `_headers`
|
|
6
|
-
// from one options object so security headers have a single source of truth
|
|
6
|
+
// from one options object so security headers have a single source of truth, (4) limits
|
|
7
|
+
// Tailwind's scan of the package components to the ones the site imports (component-sources.mjs).
|
|
7
8
|
import { copyFileSync, mkdirSync, readdirSync, writeFileSync, existsSync, readFileSync } from 'node:fs';
|
|
8
9
|
import { join, dirname, extname } from 'node:path';
|
|
9
10
|
import { fileURLToPath } from 'node:url';
|
|
10
11
|
import { createRequire } from 'node:module';
|
|
11
12
|
import { hwioRenderHeadersFile } from '@hubpav/hwio-web-worker/headers';
|
|
13
|
+
import { hwioComponentSourcesPlugin } from './component-sources.mjs';
|
|
12
14
|
|
|
13
15
|
const require = createRequire(import.meta.url);
|
|
14
16
|
|
|
@@ -19,20 +21,22 @@ function brandDir() {
|
|
|
19
21
|
const MIME = { '.woff2': 'font/woff2', '.svg': 'image/svg+xml', '.png': 'image/png', '.ico': 'image/x-icon' };
|
|
20
22
|
|
|
21
23
|
/**
|
|
22
|
-
* @param {{ headers?: import('@hubpav/hwio-web-worker/headers').HWioHeadersOptions | false, assets?: boolean, fonts?: string[] }} [options]
|
|
24
|
+
* @param {{ headers?: import('@hubpav/hwio-web-worker/headers').HWioHeadersOptions | false, assets?: boolean, fonts?: string[], componentCss?: 'used' | 'all' }} [options]
|
|
23
25
|
* fonts: font families (file-name prefixes in @hubpav/hwio-brand/dist/fonts) copied to /fonts; default ['inter'].
|
|
26
|
+
* componentCss: 'used' (default) emits CSS for the components the site imports, 'all' for every component.
|
|
24
27
|
* @returns {import('astro').AstroIntegration}
|
|
25
28
|
*/
|
|
26
29
|
export function hwioUi(options = {}) {
|
|
27
|
-
const { headers = false, assets = true, fonts = ['inter'] } = options;
|
|
30
|
+
const { headers = false, assets = true, fonts = ['inter'], componentCss = 'used' } = options;
|
|
28
31
|
const brand = brandDir();
|
|
29
32
|
const fontsDir = join(brand, 'dist/fonts');
|
|
30
33
|
const logosDir = join(brand, 'logos');
|
|
31
34
|
return {
|
|
32
35
|
name: '@hubpav/hwio-ui',
|
|
33
36
|
hooks: {
|
|
34
|
-
'astro:config:setup': ({ updateConfig }) => {
|
|
35
|
-
|
|
37
|
+
'astro:config:setup': ({ updateConfig, config, logger }) => {
|
|
38
|
+
const plugins = componentCss === 'used' ? [hwioComponentSourcesPlugin({ root: fileURLToPath(config.root), logger })] : [];
|
|
39
|
+
updateConfig({ vite: { build: { assetsInlineLimit: 0 }, plugins } });
|
|
36
40
|
},
|
|
37
41
|
'astro:server:setup': ({ server }) => {
|
|
38
42
|
if (!assets) return;
|
package/src/styles/styles.css
CHANGED
|
@@ -297,6 +297,19 @@
|
|
|
297
297
|
--menu-active-bg: transparent;
|
|
298
298
|
--menu-active-fg: currentColor;
|
|
299
299
|
}
|
|
300
|
+
/* HWioHeader navDensity="tight" (desktopFrom="xl"): from 1280 to 1535 px the container is 1280 px wide
|
|
301
|
+
while the full nav and CTAs show, and long SK/PL/CS labels overflowed it. Labels drop from 15 to 14 px
|
|
302
|
+
and item padding from 12 to 8 px in that range only; the current-page bar follows the padding.
|
|
303
|
+
Unlayered so it wins over the links' px-3 and text-[15px] utilities. */
|
|
304
|
+
@media (80rem <= width < 96rem) {
|
|
305
|
+
.hwio-nav-tight > li > :is(a, button) {
|
|
306
|
+
padding-inline: 0.5rem;
|
|
307
|
+
font-size: 14px;
|
|
308
|
+
}
|
|
309
|
+
.hwio-nav-tight > .hwio-nav-current::after {
|
|
310
|
+
inset-inline: 0.5rem;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
300
313
|
/* DaisyUI 5.7.30+ also gives pressed buttons (aria-pressed / aria-checked) a filled ground. A ghost
|
|
301
314
|
toggle shows its state in its own content (HWioThemeToggle swaps moon and sun and sets
|
|
302
315
|
aria-pressed="true" in dark mode), so it keeps the transparent ground at rest; hover and focus
|