@manjunathhk/design-tokens 1.5.1 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -10,12 +10,15 @@ This package is tokens and a small opt-in base stylesheet, nothing else:
10
10
  - design tokens for colour (light and dark), typography, spacing, radius,
11
11
  shadow, motion, layout, z-index and a breakpoint reference;
12
12
  - `base.css`, a small opt-in base (reset, body, links, selection, focus ring,
13
- reduced motion, reading defaults) built only on the tokens;
14
- - self-hosted Inter and JetBrains Mono fonts and `fonts.css`.
13
+ reduced motion, reading defaults, a content-width hook) built only on the
14
+ tokens;
15
+ - self-hosted Inter and JetBrains Mono fonts and `fonts.css`;
16
+ - `controls.js`, an opt-in script for a light/dark/system theme switch and a
17
+ Fit/Full width toggle.
15
18
 
16
19
  It is never a place for UI components (buttons, cards, nav — those are
17
20
  framework-specific and live in each site), a utility-class framework,
18
- CSS-in-JS, runtime JavaScript, or a palette switcher. See
21
+ CSS-in-JS, runtime JavaScript other than `controls.js`, or a palette switcher. See
19
22
  [AGENTS.md](AGENTS.md) for the full contract; contributors and coding agents
20
23
  read it first.
21
24
 
@@ -29,6 +32,7 @@ Each file in `dist/` has a subpath export (`@manjunathhk/design-tokens/<file>`).
29
32
  | `tokens.css` | `--mk-*` custom properties, light and dark |
30
33
  | `base.css` | Opt-in reset and base styles, zero specificity, plus `.mk-grid-bg` |
31
34
  | `fonts.css` | Self-hosted Inter and JetBrains Mono `@font-face` rules |
35
+ | `controls.js` | Opt-in theme switch and width toggle ([Site controls](#site-controls)) |
32
36
  | `tokens.json` | `{ version, light, dark, shared, breakpoints }`, flat, keyed by token path |
33
37
  | `tokens.mjs` | The same five groups as typed constants (`tokens.d.ts`); also the package root import |
34
38
  | `_tokens.scss` | `$mk-*: var(--mk-*)`, raw `$mk-breakpoint-*` values and `@include mk-media(md)` |
@@ -58,6 +62,137 @@ With no `data-theme` attribute, the page follows `prefers-color-scheme`.
58
62
  `color-scheme` is set to match on `:root` in both cases, so native form
59
63
  controls and scrollbars also switch.
60
64
 
65
+ ## Content width
66
+
67
+ Mark the main content wrapper with `data-mk-container`. `base.css` caps it at
68
+ `--mk-layout-container-max` and centres it (Fit). `data-mk-width="full"` on
69
+ `<html>` removes the cap (Full):
70
+
71
+ ```html
72
+ <html data-mk-width="full">
73
+ <!-- omit the attribute for Fit -->
74
+ <body>
75
+ <main data-mk-container>…</main>
76
+ </body>
77
+ </html>
78
+ ```
79
+
80
+ Side padding is the site's choice, for example
81
+ `padding-inline: var(--mk-layout-gutter)`. Both rules have zero specificity,
82
+ so a site's own `max-width` on the container wins without `!important`. No
83
+ JavaScript is needed: hard-code the attribute or leave it out, or let visitors
84
+ choose with [Site controls](#site-controls).
85
+
86
+ ## Site controls
87
+
88
+ `controls.js` lets visitors pick the theme (System, Light or Dark) and the
89
+ content width (Fit or Full), and remembers the choice. It is opt-in: a site
90
+ that does not load it gets the OS theme and the Fit width, and no CSS depends
91
+ on it. It has no API and adds no globals; sites only mark buttons.
92
+
93
+ ### Markup
94
+
95
+ Mark native `<button>` elements; the site supplies the contents (words, or
96
+ its own icons with an `aria-label`). The package ships no icons and no button
97
+ styles.
98
+
99
+ ```html
100
+ <div role="group" aria-label="Theme">
101
+ <button type="button" data-mk-theme-choice="system">System</button>
102
+ <button type="button" data-mk-theme-choice="light">Light</button>
103
+ <button type="button" data-mk-theme-choice="dark">Dark</button>
104
+ </div>
105
+
106
+ <button type="button" data-mk-width-toggle>Full width</button>
107
+
108
+ <main data-mk-container>…</main>
109
+ ```
110
+
111
+ | Hook | Where | Effect |
112
+ | --------------------------------------- | --------------------------- | --------------------------------------------------------------------------- |
113
+ | `data-mk-theme-choice="system"` | `<button>` | Removes `data-theme` from `<html>`, so the OS decides |
114
+ | `data-mk-theme-choice="light"`/`"dark"` | `<button>` | Sets `data-theme="light"` or `"dark"` on `<html>` |
115
+ | `data-mk-width-toggle` | `<button>` | Switches between Fit and Full |
116
+ | `data-mk-container` | Main content wrapper | Capped at `--mk-layout-container-max` (see [Content width](#content-width)) |
117
+ | `data-mk-width="full"` | `<html>`, set by the script | Removes the cap; absent means Fit |
118
+
119
+ The script keeps `aria-pressed` on every marked button in step with the
120
+ current choice, including buttons rendered later (Angular components,
121
+ WordPress blocks), so style the selected state with
122
+ `[aria-pressed="true"]`. Clicks are handled from `document`; buttons need no
123
+ `onclick` and no initialisation call.
124
+
125
+ ### Loading
126
+
127
+ Load it as a plain script in `<head>`, before the body:
128
+
129
+ ```html
130
+ <script src="https://design.manjunathhk.in/v1/controls.js"></script>
131
+ ```
132
+
133
+ Do not add `defer`, `async` or `type="module"`: any of them can run the
134
+ script after first paint, and the page flashes the OS theme and the Fit width before
135
+ the saved choice applies.
136
+
137
+ Sites that require Subresource Integrity pin a version, because the `/v1/`
138
+ alias changes content with every release:
139
+
140
+ ```html
141
+ <script
142
+ src="https://design.manjunathhk.in/v1.6.0/controls.js"
143
+ integrity="sha384-…"
144
+ crossorigin="anonymous"
145
+ ></script>
146
+ ```
147
+
148
+ Compute the hash from the pinned file, for example
149
+ `curl -s https://design.manjunathhk.in/v1.6.0/controls.js | openssl dgst -sha384 -binary | openssl base64 -A`.
150
+
151
+ ### Per stack
152
+
153
+ **Static HTML** (including Docker / NGINX): put the `<script>` tag in
154
+ `<head>`, next to the stylesheet `<link>`.
155
+
156
+ **WordPress**, in `functions.php`. Load it in the header and give no
157
+ `strategy`, so WordPress adds neither `defer` nor `async` (the `$args` array
158
+ needs WordPress 6.3 or later; on older versions pass `false`):
159
+
160
+ ```php
161
+ function mk_design_tokens_controls_enqueue() {
162
+ wp_enqueue_script(
163
+ 'mk-design-tokens-controls',
164
+ 'https://design.manjunathhk.in/v1/controls.js',
165
+ [],
166
+ null,
167
+ [ 'in_footer' => false ]
168
+ );
169
+ }
170
+ add_action( 'wp_enqueue_scripts', 'mk_design_tokens_controls_enqueue' );
171
+ ```
172
+
173
+ **.NET Razor or Blazor**: in the shared layout's `<head>` (`_Layout.cshtml`
174
+ or `App.razor`), next to the stylesheet `<link>`.
175
+
176
+ **Angular**: put the `<script>` tag in the `<head>` of `src/index.html`. Do
177
+ not list the file in the `scripts` array of `angular.json`: Angular injects
178
+ those at the end of `<body>`, which makes the page flash. To serve it from
179
+ the npm package instead of the CDN, copy it with an `assets` entry in
180
+ `angular.json`:
181
+
182
+ ```json
183
+ { "glob": "controls.js", "input": "node_modules/@manjunathhk/design-tokens/dist", "output": "/" }
184
+ ```
185
+
186
+ and reference `<script src="controls.js"></script>` from the `<head>` of
187
+ `src/index.html`.
188
+
189
+ ### Storage
190
+
191
+ The choices are stored in `localStorage` under `mk-theme` and `mk-width`.
192
+ Storage is per origin, so a choice made on `example.com` does not carry to
193
+ `blog.example.com`. If storage is unavailable (blocked, private mode), the
194
+ defaults apply and a choice lasts until the page is left.
195
+
61
196
  ## Consumption
62
197
 
63
198
  Prefer the CDN alias for sites without a build step; it always serves the
@@ -164,8 +299,12 @@ unstyled content.
164
299
  ```
165
300
  style-src 'self' https://design.manjunathhk.in;
166
301
  font-src 'self' https://design.manjunathhk.in;
302
+ script-src 'self' https://design.manjunathhk.in;
167
303
  ```
168
304
 
305
+ `script-src` is needed only on a site that loads `controls.js`. There is no
306
+ inline snippet, so no `'unsafe-inline'`, hash or nonce is required.
307
+
169
308
  Add `https://cdn.jsdelivr.net` to both directives only on a site that is
170
309
  actively using the jsDelivr fallback above.
171
310
 
@@ -246,6 +385,8 @@ on the `promote` workflow, which accepts a final `X.Y.Z` version only.
246
385
  </html>
247
386
  ```
248
387
 
388
+ To let visitors choose, see [Site controls](#site-controls).
389
+
249
390
  ## Migration map
250
391
 
251
392
  From the portfolio's previous local variable names:
package/dist/_tokens.scss CHANGED
@@ -1,4 +1,4 @@
1
- /*! @manjunathhk/design-tokens v1.5.1 */
1
+ /*! @manjunathhk/design-tokens v1.6.0 */
2
2
  @use "sass:map";
3
3
 
4
4
  $mk-color-accent: var(--mk-color-accent);
package/dist/base.css CHANGED
@@ -1,4 +1,4 @@
1
- /*! @manjunathhk/design-tokens v1.5.1 */
1
+ /*! @manjunathhk/design-tokens v1.6.0 */
2
2
  /*
3
3
  * Opt-in base built only on the tokens. Selectors sit inside :where() so they
4
4
  * carry zero specificity and any site rule overrides them.
@@ -68,6 +68,16 @@
68
68
  font-family: var(--mk-font-family-mono);
69
69
  }
70
70
 
71
+ /* Content width (D54): Fit caps the container; data-mk-width="full" on <html> removes the cap. */
72
+ :where([data-mk-container]) {
73
+ max-width: var(--mk-layout-container-max);
74
+ margin-inline: auto;
75
+ }
76
+
77
+ :where(html[data-mk-width="full"] [data-mk-container]) {
78
+ max-width: none;
79
+ }
80
+
71
81
  /* Links */
72
82
  :where(a) {
73
83
  color: var(--mk-color-accent);
@@ -0,0 +1,87 @@
1
+ /*! @manjunathhk/design-tokens v1.6.0 */
2
+ "use strict";
3
+ /*
4
+ * Site controls (D54): a three-state theme switch and a Fit/Full width toggle.
5
+ * Load as a classic, blocking script in <head> so the saved choice applies
6
+ * before first paint, then mark native buttons:
7
+ * <button type="button" data-mk-theme-choice="system|light|dark">
8
+ * <button type="button" data-mk-width-toggle>
9
+ */
10
+ (() => {
11
+ const THEME_KEY = "mk-theme";
12
+ const WIDTH_KEY = "mk-width";
13
+ const THEME_BUTTONS = {
14
+ system: '[data-mk-theme-choice="system"]',
15
+ light: '[data-mk-theme-choice="light"]',
16
+ dark: '[data-mk-theme-choice="dark"]',
17
+ };
18
+ const WIDTH_TOGGLE = "[data-mk-width-toggle]";
19
+ const THEMES = Object.keys(THEME_BUTTONS);
20
+ const root = document.documentElement;
21
+ // Storage can be missing or throw (private mode, blocked cookies); the
22
+ // defaults then apply and a choice lasts until the page is left.
23
+ function load(key) {
24
+ try {
25
+ return localStorage.getItem(key);
26
+ }
27
+ catch {
28
+ return null;
29
+ }
30
+ }
31
+ function save(key, value) {
32
+ try {
33
+ localStorage.setItem(key, value);
34
+ }
35
+ catch {
36
+ // Not stored; the choice still applies to this page.
37
+ }
38
+ }
39
+ const storedTheme = load(THEME_KEY);
40
+ let theme = storedTheme === "light" || storedTheme === "dark" ? storedTheme : "system";
41
+ let full = load(WIDTH_KEY) === "full";
42
+ function press(selector, pressed) {
43
+ const value = String(pressed);
44
+ for (const button of document.querySelectorAll(selector)) {
45
+ if (button.getAttribute("aria-pressed") !== value)
46
+ button.setAttribute("aria-pressed", value);
47
+ }
48
+ }
49
+ function sync() {
50
+ for (const choice of THEMES)
51
+ press(THEME_BUTTONS[choice], choice === theme);
52
+ press(WIDTH_TOGGLE, full);
53
+ }
54
+ function apply() {
55
+ if (theme === "system")
56
+ root.removeAttribute("data-theme");
57
+ else
58
+ root.setAttribute("data-theme", theme);
59
+ if (full)
60
+ root.setAttribute("data-mk-width", "full");
61
+ else
62
+ root.removeAttribute("data-mk-width");
63
+ sync();
64
+ }
65
+ apply();
66
+ document.addEventListener("click", (event) => {
67
+ if (!(event.target instanceof Element))
68
+ return;
69
+ const target = event.target;
70
+ const choice = THEMES.find((c) => target.closest(THEME_BUTTONS[c]));
71
+ if (choice) {
72
+ theme = choice;
73
+ save(THEME_KEY, choice);
74
+ }
75
+ else if (target.closest(WIDTH_TOGGLE)) {
76
+ full = !full;
77
+ save(WIDTH_KEY, full ? "full" : "fit");
78
+ }
79
+ else {
80
+ return;
81
+ }
82
+ apply();
83
+ });
84
+ // Buttons parsed or rendered after this script (the page body, Angular,
85
+ // WordPress blocks) get the current aria-pressed as soon as they appear.
86
+ new MutationObserver(sync).observe(root, { childList: true, subtree: true });
87
+ })();
package/dist/fonts.css CHANGED
@@ -1,4 +1,4 @@
1
- /*! @manjunathhk/design-tokens v1.5.1 */
1
+ /*! @manjunathhk/design-tokens v1.6.0 */
2
2
  @font-face {
3
3
  font-family: "Inter";
4
4
  font-style: normal;
package/dist/index.css CHANGED
@@ -1,4 +1,4 @@
1
- /*! @manjunathhk/design-tokens v1.5.1 */
1
+ /*! @manjunathhk/design-tokens v1.6.0 */
2
2
  @font-face {
3
3
  font-family: "Inter";
4
4
  font-style: normal;
@@ -276,6 +276,16 @@
276
276
  font-family: var(--mk-font-family-mono);
277
277
  }
278
278
 
279
+ /* Content width (D54): Fit caps the container; data-mk-width="full" on <html> removes the cap. */
280
+ :where([data-mk-container]) {
281
+ max-width: var(--mk-layout-container-max);
282
+ margin-inline: auto;
283
+ }
284
+
285
+ :where(html[data-mk-width="full"] [data-mk-container]) {
286
+ max-width: none;
287
+ }
288
+
279
289
  /* Links */
280
290
  :where(a) {
281
291
  color: var(--mk-color-accent);
@@ -1,4 +1,4 @@
1
- /*! @manjunathhk/design-tokens v1.5.1 */
1
+ /*! @manjunathhk/design-tokens v1.6.0 */
2
2
  import type { Config } from "stylelint";
3
3
 
4
4
  declare const config: Config;
@@ -1,4 +1,4 @@
1
- /*! @manjunathhk/design-tokens v1.5.1 */
1
+ /*! @manjunathhk/design-tokens v1.6.0 */
2
2
  export default {
3
3
  rules: {
4
4
  "color-no-hex": true,
package/dist/tokens.css CHANGED
@@ -1,4 +1,4 @@
1
- /*! @manjunathhk/design-tokens v1.5.1 */
1
+ /*! @manjunathhk/design-tokens v1.6.0 */
2
2
  :root {
3
3
  --mk-color-accent: #B45309;
4
4
  --mk-color-accent-hover: #92400E;
package/dist/tokens.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- /*! @manjunathhk/design-tokens v1.5.1 */
1
+ /*! @manjunathhk/design-tokens v1.6.0 */
2
2
  /** Colour tokens; light and dark share these keys. */
3
3
  export interface ColorTokens {
4
4
  readonly "color.accent": string;
package/dist/tokens.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.5.1",
2
+ "version": "1.6.0",
3
3
  "light": {
4
4
  "color.accent": "#B45309",
5
5
  "color.accent-hover": "#92400E",
package/dist/tokens.mjs CHANGED
@@ -1,5 +1,5 @@
1
- /*! @manjunathhk/design-tokens v1.5.1 */
2
- export const version = "1.5.1";
1
+ /*! @manjunathhk/design-tokens v1.6.0 */
2
+ export const version = "1.6.0";
3
3
  export const light = {
4
4
  "color.accent": "#B45309",
5
5
  "color.accent-hover": "#92400E",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@manjunathhk/design-tokens",
3
- "version": "1.5.1",
3
+ "version": "1.6.0",
4
4
  "description": "Design tokens and a small opt-in base stylesheet shared by Manjunath's websites.",
5
5
  "license": "MIT",
6
6
  "author": "Manjunath Krishnasa",
@@ -23,6 +23,7 @@
23
23
  "./tokens.css": "./dist/tokens.css",
24
24
  "./base.css": "./dist/base.css",
25
25
  "./fonts.css": "./dist/fonts.css",
26
+ "./controls.js": "./dist/controls.js",
26
27
  "./tokens.json": "./dist/tokens.json",
27
28
  "./tokens.mjs": {
28
29
  "types": "./dist/tokens.d.ts",