@littlepress/renderer-web 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 +202 -0
- package/NOTICE +20 -0
- package/README.md +229 -2
- package/dist/answer/answerComponents.d.ts +107 -0
- package/dist/answer/answerComponents.js +637 -0
- package/dist/answer/download.d.ts +63 -0
- package/dist/answer/download.js +109 -0
- package/dist/answer/downloadExport.d.ts +44 -0
- package/dist/answer/downloadExport.js +266 -0
- package/dist/answer/hostValues.d.ts +28 -0
- package/dist/answer/hostValues.js +379 -0
- package/dist/answer/mapComponent.d.ts +6 -0
- package/dist/answer/mapComponent.js +213 -0
- package/dist/answer/math.d.ts +57 -0
- package/dist/answer/math.js +168 -0
- package/dist/answer/surfaceStream.d.ts +51 -0
- package/dist/answer/surfaceStream.js +260 -0
- package/dist/answer/tableLayout.d.ts +42 -0
- package/dist/answer/tableLayout.js +89 -0
- package/dist/controls/ActionFeedback.d.ts +5 -0
- package/dist/controls/ActionFeedback.js +43 -0
- package/dist/controls/actionDispatch.d.ts +51 -0
- package/dist/controls/actionDispatch.js +211 -0
- package/dist/controls/actionOutcome.generated.d.ts +64 -0
- package/dist/controls/actionOutcome.generated.js +64 -0
- package/dist/controls/calculationPublisher.d.ts +14 -0
- package/dist/controls/calculationPublisher.js +6 -0
- package/dist/controls/calculations.d.ts +20 -0
- package/dist/controls/calculations.generated.d.ts +73 -0
- package/dist/controls/calculations.generated.js +85 -0
- package/dist/controls/calculations.js +208 -0
- package/dist/controls/controlState.d.ts +84 -0
- package/dist/controls/controlState.js +256 -0
- package/dist/controls/fieldMessages.d.ts +41 -0
- package/dist/controls/fieldMessages.js +81 -0
- package/dist/controls/fieldValidation.d.ts +19 -0
- package/dist/controls/fieldValidation.js +98 -0
- package/dist/controls/formComponents.d.ts +23 -0
- package/dist/controls/formComponents.js +738 -0
- package/dist/controls/formProgress.d.ts +63 -0
- package/dist/controls/formProgress.js +122 -0
- package/dist/controls/inputDrafts.d.ts +55 -0
- package/dist/controls/inputDrafts.js +183 -0
- package/dist/controls/inputValidation.d.ts +33 -0
- package/dist/controls/inputValidation.js +119 -0
- package/dist/controls/localWrites.d.ts +9 -0
- package/dist/controls/localWrites.js +29 -0
- package/dist/core/A2uiSurface.d.ts +13 -0
- package/dist/core/A2uiSurface.js +15 -0
- package/dist/core/AtlasIcon.d.ts +18 -0
- package/dist/core/AtlasIcon.js +22 -0
- package/dist/core/AtlasMedia.d.ts +22 -0
- package/dist/core/AtlasMedia.js +45 -0
- package/dist/core/Carousel.d.ts +61 -0
- package/dist/core/Carousel.js +183 -0
- package/dist/core/CoreMedia.d.ts +19 -0
- package/dist/core/CoreMedia.js +25 -0
- package/dist/core/FoundationA2uiSurface.d.ts +86 -0
- package/dist/core/FoundationA2uiSurface.js +440 -0
- package/dist/core/ImagePreview.d.ts +42 -0
- package/dist/core/ImagePreview.js +73 -0
- package/dist/core/LucideIcon.d.ts +19 -0
- package/dist/core/LucideIcon.js +42 -0
- package/dist/core/SurfaceLink.d.ts +35 -0
- package/dist/core/SurfaceLink.js +60 -0
- package/dist/core/appearance/aegis.d.ts +49 -0
- package/dist/core/appearance/aegis.js +34 -0
- package/dist/core/appearance/colour.d.ts +24 -0
- package/dist/core/appearance/colour.js +103 -0
- package/dist/core/appearance/css.d.ts +17 -0
- package/dist/core/appearance/css.js +153 -0
- package/dist/core/appearance/presets.generated.d.ts +4 -0
- package/dist/core/appearance/presets.generated.js +1184 -0
- package/dist/core/appearance/react.d.ts +29 -0
- package/dist/core/appearance/react.js +110 -0
- package/dist/core/appearance/resolve.d.ts +76 -0
- package/dist/core/appearance/resolve.js +127 -0
- package/dist/core/appearance/textScale.d.ts +11 -0
- package/dist/core/appearance/textScale.js +51 -0
- package/dist/core/appearance/theme.d.ts +138 -0
- package/dist/core/appearance/theme.js +274 -0
- package/dist/core/baseComponents.d.ts +53 -0
- package/dist/core/baseComponents.js +480 -0
- package/dist/core/binding.d.ts +46 -0
- package/dist/core/binding.js +118 -0
- package/dist/core/bundle.d.ts +23 -0
- package/dist/core/bundle.js +75 -0
- package/dist/core/catalog/catalog-index.generated.d.ts +44 -0
- package/dist/core/catalog/catalog-index.generated.js +79 -0
- package/dist/core/catalog/icons.generated.d.ts +6 -0
- package/dist/core/catalog/icons.generated.js +1788 -0
- package/dist/core/catalog.d.ts +6 -0
- package/dist/core/catalog.generated.d.ts +50 -0
- package/dist/core/catalog.generated.js +2174 -0
- package/dist/core/catalog.js +8 -0
- package/dist/core/chartRows.d.ts +7 -0
- package/dist/core/chartRows.js +38 -0
- package/dist/core/component-support.d.ts +39 -0
- package/dist/core/component-support.js +127 -0
- package/dist/core/components/actions.d.ts +4 -0
- package/dist/core/components/actions.js +21 -0
- package/dist/core/components/cards.d.ts +9 -0
- package/dist/core/components/cards.js +227 -0
- package/dist/core/components/charts.d.ts +18 -0
- package/dist/core/components/charts.js +412 -0
- package/dist/core/components/comparisonTable.d.ts +50 -0
- package/dist/core/components/comparisonTable.js +89 -0
- package/dist/core/components/content.d.ts +13 -0
- package/dist/core/components/content.js +108 -0
- package/dist/core/components/seriesCharts.d.ts +531 -0
- package/dist/core/components/seriesCharts.js +1053 -0
- package/dist/core/components/stepFlow.d.ts +7 -0
- package/dist/core/components/stepFlow.js +13 -0
- package/dist/core/components.full.d.ts +10 -0
- package/dist/core/components.full.js +41 -0
- package/dist/core/context.d.ts +207 -0
- package/dist/core/context.js +172 -0
- package/dist/core/coverage.d.ts +30 -0
- package/dist/core/coverage.js +93 -0
- package/dist/core/foundation.d.ts +21 -0
- package/dist/core/foundation.js +22 -0
- package/dist/core/fullTheme.d.ts +5 -0
- package/dist/core/fullTheme.js +9 -0
- package/dist/core/iconNodes.generated.d.ts +7 -0
- package/dist/core/iconNodes.generated.js +5 -0
- package/dist/core/lucideIcons.d.ts +5 -0
- package/dist/core/lucideIcons.js +7 -0
- package/dist/core/messages.d.ts +111 -0
- package/dist/core/messages.js +134 -0
- package/dist/core/pointer.d.ts +17 -0
- package/dist/core/pointer.js +104 -0
- package/dist/core/registry.d.ts +12 -0
- package/dist/core/registry.js +114 -0
- package/dist/core/runtime.d.ts +8 -0
- package/dist/core/runtime.js +6 -0
- package/dist/core/state.d.ts +73 -0
- package/dist/core/state.js +274 -0
- package/dist/core/surfaceMotion.d.ts +5 -0
- package/dist/core/surfaceMotion.js +46 -0
- package/dist/core/theme.d.ts +5 -0
- package/dist/core/theme.js +7 -0
- package/dist/core/useSurfaceImage.d.ts +20 -0
- package/dist/core/useSurfaceImage.js +65 -0
- package/dist/graph/RelationshipGraph.d.ts +14 -0
- package/dist/graph/RelationshipGraph.js +206 -0
- package/dist/graph/model.d.ts +20 -0
- package/dist/graph/model.js +36 -0
- package/dist/graph/program/assets.d.ts +3 -0
- package/dist/graph/program/assets.js +3 -0
- package/dist/graph/program/bridge.d.ts +6 -0
- package/dist/graph/program/bridge.generated.d.ts +136 -0
- package/dist/graph/program/bridge.generated.js +1 -0
- package/dist/graph/program/bridge.js +24 -0
- package/dist/graph/program/bridge.schema.json +717 -0
- package/dist/graph/program/bridgeValidator.cjs +2 -0
- package/dist/graph/program/bridgeValidator.d.cts +4 -0
- package/dist/graph/program/runner.d.ts +1 -0
- package/dist/graph/program/runner.html +2193 -0
- package/dist/graph/program/runner.js +157 -0
- package/dist/graph/program/runtime-manifest.json +1 -0
- package/dist/graph/program/view.d.ts +11 -0
- package/dist/graph/program/view.js +210 -0
- package/dist/graph/program/worker.d.ts +1 -0
- package/dist/graph/program/worker.js +2186 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +45 -0
- package/dist/internal/dsh.d.ts +4 -0
- package/dist/internal/dsh.js +3 -0
- package/dist/internal/relationship-graph.d.ts +4 -0
- package/dist/internal/relationship-graph.js +7202 -0
- package/dist/page/layoutComponents.d.ts +6 -0
- package/dist/page/layoutComponents.js +123 -0
- package/dist/page/layoutGeometry.d.ts +68 -0
- package/dist/page/layoutGeometry.js +174 -0
- package/dist/page/pageFormat.d.ts +34 -0
- package/dist/page/pageFormat.js +119 -0
- package/dist/page/printFit.d.ts +81 -0
- package/dist/page/printFit.js +338 -0
- package/dist/react/index.d.ts +5 -0
- package/dist/react/index.js +2 -0
- package/dist/report/locate.d.ts +42 -0
- package/dist/report/locate.js +34 -0
- package/dist/styles/renderer.css +4580 -0
- package/licenses/LUCIDE-LICENSE.txt +43 -0
- package/licenses/QUICKJS-EMSCRIPTEN-LICENSE.txt +21 -0
- package/licenses/QUICKJS-VARIANT-LICENSE.txt +47 -0
- package/package.json +68 -3
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import { type RenderContext } from "./context.ts";
|
|
3
|
+
import type { A2uiComponent } from "./messages.ts";
|
|
4
|
+
export type SurfaceLinkMode = "host" | "default" | "inert";
|
|
5
|
+
export declare function resolveSurfaceLink(rawUrl: unknown, action: unknown, component: A2uiComponent, context: RenderContext, extraPayload?: Record<string, unknown>, actionKey?: string): {
|
|
6
|
+
mode: SurfaceLinkMode;
|
|
7
|
+
safeUrl: null;
|
|
8
|
+
activate: undefined;
|
|
9
|
+
} | {
|
|
10
|
+
mode: SurfaceLinkMode;
|
|
11
|
+
safeUrl: string;
|
|
12
|
+
activate: import("./context.ts").ActionClickHandler | undefined;
|
|
13
|
+
};
|
|
14
|
+
export type SurfaceLinkProps = {
|
|
15
|
+
url: unknown;
|
|
16
|
+
action: unknown;
|
|
17
|
+
component: A2uiComponent;
|
|
18
|
+
context: RenderContext;
|
|
19
|
+
actionKey: string;
|
|
20
|
+
extraPayload?: Record<string, unknown>;
|
|
21
|
+
className: string;
|
|
22
|
+
children: ReactNode;
|
|
23
|
+
dataAttributes?: Record<string, string | undefined>;
|
|
24
|
+
inertTag?: "fragment" | "div" | "span";
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Unified link opening control across inline spans, citations, entities, and sources:
|
|
28
|
+
* - Rejects executable schemes (`javascript:`, `vbscript:`, `data:`) as inert text/container.
|
|
29
|
+
* - Renders `<button type="button">` (no native `<a href>`) when the Host `actionHandler`
|
|
30
|
+
* accepts `openUrl`, preventing right-click / middle-click from bypassing Host rejection.
|
|
31
|
+
* - Renders `<a href>` with `noopener,noreferrer` and `referrerPolicy="no-referrer"` on
|
|
32
|
+
* `http(s)` (and native `<a href>` on `mailto:` / `tel:`) when the Host declines.
|
|
33
|
+
*/
|
|
34
|
+
export declare function renderSurfaceLink({ url, action, component, context, actionKey, extraPayload, className, children, dataAttributes, inertTag, }: SurfaceLinkProps): import("react/jsx-runtime").JSX.Element;
|
|
35
|
+
export declare function SurfaceLink(props: SurfaceLinkProps): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { isDefaultOpenableLinkUrl, validLinkUrl } from "../answer/hostValues.js";
|
|
3
|
+
import { surfaceIsStreaming } from "../controls/actionDispatch.js";
|
|
4
|
+
import { actionHandler, resolveAction } from "./context.js";
|
|
5
|
+
export function resolveSurfaceLink(rawUrl, action, component, context, extraPayload = {}, actionKey) {
|
|
6
|
+
const safeUrl = validLinkUrl(rawUrl);
|
|
7
|
+
if (!safeUrl) {
|
|
8
|
+
return { mode: "inert", safeUrl: null, activate: undefined };
|
|
9
|
+
}
|
|
10
|
+
const resolved = resolveAction(action, component, context, extraPayload, actionKey);
|
|
11
|
+
const activate = actionHandler(action, component, context, extraPayload, actionKey);
|
|
12
|
+
const hostHandled = Boolean(resolved && context.actionSession?.hostAccepts(resolved));
|
|
13
|
+
if (hostHandled) {
|
|
14
|
+
return { mode: "host", safeUrl, activate };
|
|
15
|
+
}
|
|
16
|
+
if (isDefaultOpenableLinkUrl(safeUrl)) {
|
|
17
|
+
return { mode: "default", safeUrl, activate };
|
|
18
|
+
}
|
|
19
|
+
return { mode: "inert", safeUrl: null, activate: undefined };
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Unified link opening control across inline spans, citations, entities, and sources:
|
|
23
|
+
* - Rejects executable schemes (`javascript:`, `vbscript:`, `data:`) as inert text/container.
|
|
24
|
+
* - Renders `<button type="button">` (no native `<a href>`) when the Host `actionHandler`
|
|
25
|
+
* accepts `openUrl`, preventing right-click / middle-click from bypassing Host rejection.
|
|
26
|
+
* - Renders `<a href>` with `noopener,noreferrer` and `referrerPolicy="no-referrer"` on
|
|
27
|
+
* `http(s)` (and native `<a href>` on `mailto:` / `tel:`) when the Host declines.
|
|
28
|
+
*/
|
|
29
|
+
export function renderSurfaceLink({ url, action, component, context, actionKey, extraPayload, className, children, dataAttributes, inertTag = "fragment", }) {
|
|
30
|
+
const { mode, safeUrl, activate } = resolveSurfaceLink(url, action, component, context, extraPayload, actionKey);
|
|
31
|
+
if (mode === "inert" || !safeUrl) {
|
|
32
|
+
if (inertTag === "div") {
|
|
33
|
+
return _jsx("div", { className: className, ...dataAttributes, children: children });
|
|
34
|
+
}
|
|
35
|
+
if (inertTag === "span") {
|
|
36
|
+
return _jsx("span", { className: className, ...dataAttributes, children: children });
|
|
37
|
+
}
|
|
38
|
+
return _jsx(_Fragment, { children: children });
|
|
39
|
+
}
|
|
40
|
+
const disabled = !activate || activate.disabled === true;
|
|
41
|
+
if (surfaceIsStreaming(context.state)) {
|
|
42
|
+
return (_jsx("span", { className: className, ...dataAttributes, "data-pending-link": "true", "aria-disabled": "true", children: children }));
|
|
43
|
+
}
|
|
44
|
+
if (mode === "host") {
|
|
45
|
+
return (_jsx("button", { type: "button", className: className, ...dataAttributes, "data-action-key": actionKey, disabled: disabled, "aria-disabled": disabled ? true : undefined, "aria-busy": activate?.pending ? true : undefined, onClick: () => {
|
|
46
|
+
if (!disabled)
|
|
47
|
+
activate?.();
|
|
48
|
+
}, children: children }));
|
|
49
|
+
}
|
|
50
|
+
const lower = safeUrl.toLowerCase();
|
|
51
|
+
const isHttp = lower.startsWith("http://") || lower.startsWith("https://");
|
|
52
|
+
return (_jsx("a", { className: className, ...dataAttributes, href: safeUrl, target: isHttp ? "_blank" : undefined, rel: isHttp ? "noopener noreferrer" : undefined, referrerPolicy: isHttp ? "no-referrer" : undefined, "data-action-key": actionKey, "aria-disabled": disabled ? true : undefined, "aria-busy": activate?.pending ? true : undefined, onClick: (event) => {
|
|
53
|
+
event.preventDefault();
|
|
54
|
+
if (!disabled)
|
|
55
|
+
activate?.();
|
|
56
|
+
}, children: children }));
|
|
57
|
+
}
|
|
58
|
+
export function SurfaceLink(props) {
|
|
59
|
+
return renderSurfaceLink(props);
|
|
60
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { ResolvedAppearance } from "./resolve.ts";
|
|
2
|
+
/**
|
|
3
|
+
* The appearance an `aegis-quickjs-ui` program receives (`environment.appearance`): final CSS px and
|
|
4
|
+
* colours already chosen for light or dark. The shape and ranges are the Aegis UI runtime's
|
|
5
|
+
* (docs/architecture/surface-pipeline.md).
|
|
6
|
+
*/
|
|
7
|
+
export type AegisAppearanceValues = {
|
|
8
|
+
color: {
|
|
9
|
+
bg: string;
|
|
10
|
+
surface: string;
|
|
11
|
+
border: string;
|
|
12
|
+
text: string;
|
|
13
|
+
muted: string;
|
|
14
|
+
accent: string;
|
|
15
|
+
accentText: string;
|
|
16
|
+
positive: string;
|
|
17
|
+
danger: string;
|
|
18
|
+
warning: string;
|
|
19
|
+
track: string;
|
|
20
|
+
};
|
|
21
|
+
text: {
|
|
22
|
+
body: number;
|
|
23
|
+
title: number;
|
|
24
|
+
heading: number;
|
|
25
|
+
caption: number;
|
|
26
|
+
};
|
|
27
|
+
boldText: boolean;
|
|
28
|
+
minTouchTarget: number;
|
|
29
|
+
controlHeight: number;
|
|
30
|
+
radius: {
|
|
31
|
+
control: number;
|
|
32
|
+
card: number;
|
|
33
|
+
pill: number;
|
|
34
|
+
};
|
|
35
|
+
space: {
|
|
36
|
+
gap: number;
|
|
37
|
+
cardPadding: number;
|
|
38
|
+
cardGap: number;
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* A resolved appearance as an Aegis program's appearance, so programs draw with the cards'
|
|
43
|
+
* colours, sizes and shapes. Every number is kept inside the runtime's range: a very large text
|
|
44
|
+
* scale gives the largest control the runtime accepts instead of a program that cannot start.
|
|
45
|
+
*
|
|
46
|
+
* @param cssPx Turns a text size already through the text scale curve into CSS px. Web text is
|
|
47
|
+
* already CSS px; Android passes its `sp` conversion, which adds the system font size.
|
|
48
|
+
*/
|
|
49
|
+
export declare function aegisAppearance(resolved: ResolvedAppearance, cssPx?: (size: number) => number): AegisAppearanceValues;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { over } from "./colour.js";
|
|
2
|
+
import { scaleTextSize } from "./textScale.js";
|
|
3
|
+
const clamp = (value, min, max) => Number.isFinite(value) ? Math.round(Math.min(max, Math.max(min, value)) * 100) / 100 : min;
|
|
4
|
+
/**
|
|
5
|
+
* A resolved appearance as an Aegis program's appearance, so programs draw with the cards'
|
|
6
|
+
* colours, sizes and shapes. Every number is kept inside the runtime's range: a very large text
|
|
7
|
+
* scale gives the largest control the runtime accepts instead of a program that cannot start.
|
|
8
|
+
*
|
|
9
|
+
* @param cssPx Turns a text size already through the text scale curve into CSS px. Web text is
|
|
10
|
+
* already CSS px; Android passes its `sp` conversion, which adds the system font size.
|
|
11
|
+
*/
|
|
12
|
+
export function aegisAppearance(resolved, cssPx = (size) => size) {
|
|
13
|
+
const c = resolved.color;
|
|
14
|
+
const text = (role) => clamp(cssPx(scaleTextSize(resolved.type[role].size, resolved.textScale)), 8, 96);
|
|
15
|
+
const body = resolved.type.bodyMedium.size;
|
|
16
|
+
// Text-sized parts grow by the ratio body text grows by, as the cards' controls do.
|
|
17
|
+
const grow = scaleTextSize(body, resolved.textScale) / body;
|
|
18
|
+
return {
|
|
19
|
+
// The program sits on a card, so its background is the card surface as it shows: painted over the
|
|
20
|
+
// page background (a theme may make the card see-through), never a translucent colour the
|
|
21
|
+
// runtime would put on white.
|
|
22
|
+
color: {
|
|
23
|
+
bg: over(c.surface, c.background), surface: c.surfaceVariant, border: c.outline, text: c.text, muted: c.textVariant,
|
|
24
|
+
accent: c.accent, accentText: c.onAccent, positive: c.positive, danger: c.danger, warning: c.warning,
|
|
25
|
+
track: c.surfaceVariant,
|
|
26
|
+
},
|
|
27
|
+
text: { body: text("bodyMedium"), title: text("titleLarge"), heading: text("titleSmall"), caption: text("labelSmall") },
|
|
28
|
+
boldText: resolved.boldText,
|
|
29
|
+
minTouchTarget: clamp(resolved.minTouchTarget, 16, 128),
|
|
30
|
+
controlHeight: clamp(resolved.size.control * grow, 16, 128),
|
|
31
|
+
radius: { control: clamp(resolved.radius.control, 0, 999), card: clamp(resolved.radius.surfaceLarge, 0, 999), pill: clamp(resolved.radius.pill, 0, 999) },
|
|
32
|
+
space: { gap: clamp(resolved.space.sm, 0, 64), cardPadding: clamp(resolved.space.md, 0, 64), cardGap: clamp(resolved.space.xs, 0, 64) },
|
|
33
|
+
};
|
|
34
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Colour maths shared by the theme check and the display settings. Every step is defined on
|
|
3
|
+
* 8-bit sRGB channels so Web, Compose and SwiftUI reach the same hex values
|
|
4
|
+
* (ui-foundation/surface/contracts/fixtures/renderer/appearance.json).
|
|
5
|
+
*/
|
|
6
|
+
export type Rgba = readonly [number, number, number, number];
|
|
7
|
+
export declare function isHex(value: unknown): value is string;
|
|
8
|
+
export declare function isHexAlpha(value: unknown): value is string;
|
|
9
|
+
export declare function parseHex(value: string): Rgba;
|
|
10
|
+
export declare function toHex(colour: Rgba): string;
|
|
11
|
+
/** `amount` of `top` over `base`, both opaque: the CSS `color-mix(in srgb, top amount%, base)`. */
|
|
12
|
+
export declare function mix(top: string, base: string, amount: number): string;
|
|
13
|
+
/** `top` painted over the opaque `base`, as the browser composites a translucent colour. */
|
|
14
|
+
export declare function over(top: string, base: string): string;
|
|
15
|
+
/** The same colour with a fixed alpha, written `#RRGGBBAA`. */
|
|
16
|
+
export declare function withAlpha(colour: string, alpha: number): string;
|
|
17
|
+
/** WCAG 2 contrast ratio between two opaque colours. */
|
|
18
|
+
export declare function contrastRatio(a: string, b: string): number;
|
|
19
|
+
/**
|
|
20
|
+
* Darken (`direction` -1) or lighten (+1) `colour` in HSL lightness steps of 0.01, keeping hue and
|
|
21
|
+
* saturation, until it reaches `minimum` against every colour in `against`. Returns the first step
|
|
22
|
+
* that does, or the last step tried when lightness runs out.
|
|
23
|
+
*/
|
|
24
|
+
export declare function adjustForContrast(colour: string, against: readonly string[], minimum: number, direction: 1 | -1): string;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Colour maths shared by the theme check and the display settings. Every step is defined on
|
|
3
|
+
* 8-bit sRGB channels so Web, Compose and SwiftUI reach the same hex values
|
|
4
|
+
* (ui-foundation/surface/contracts/fixtures/renderer/appearance.json).
|
|
5
|
+
*/
|
|
6
|
+
const HEX = /^#([0-9a-f]{6})([0-9a-f]{2})?$/i;
|
|
7
|
+
export function isHex(value) {
|
|
8
|
+
return typeof value === "string" && /^#[0-9a-f]{6}$/i.test(value);
|
|
9
|
+
}
|
|
10
|
+
export function isHexAlpha(value) {
|
|
11
|
+
return typeof value === "string" && HEX.test(value);
|
|
12
|
+
}
|
|
13
|
+
export function parseHex(value) {
|
|
14
|
+
const match = HEX.exec(value);
|
|
15
|
+
if (!match)
|
|
16
|
+
throw new Error(`not a colour: ${value}`);
|
|
17
|
+
const n = Number.parseInt(match[1], 16);
|
|
18
|
+
const alpha = match[2] === undefined ? 1 : Number.parseInt(match[2], 16) / 255;
|
|
19
|
+
return [(n >> 16) & 255, (n >> 8) & 255, n & 255, alpha];
|
|
20
|
+
}
|
|
21
|
+
const channel = (x) => Math.round(Math.max(0, Math.min(255, x))).toString(16).padStart(2, "0").toUpperCase();
|
|
22
|
+
export function toHex(colour) {
|
|
23
|
+
const rgb = `#${channel(colour[0])}${channel(colour[1])}${channel(colour[2])}`;
|
|
24
|
+
return colour[3] >= 0.9995 ? rgb : `${rgb}${channel(colour[3] * 255)}`;
|
|
25
|
+
}
|
|
26
|
+
/** `amount` of `top` over `base`, both opaque: the CSS `color-mix(in srgb, top amount%, base)`. */
|
|
27
|
+
export function mix(top, base, amount) {
|
|
28
|
+
const a = parseHex(top);
|
|
29
|
+
const b = parseHex(base);
|
|
30
|
+
return toHex([0, 1, 2].map((i) => a[i] * amount + b[i] * (1 - amount)).concat(1));
|
|
31
|
+
}
|
|
32
|
+
/** `top` painted over the opaque `base`, as the browser composites a translucent colour. */
|
|
33
|
+
export function over(top, base) {
|
|
34
|
+
const a = parseHex(top);
|
|
35
|
+
const b = parseHex(base);
|
|
36
|
+
return toHex([0, 1, 2].map((i) => a[i] * a[3] + b[i] * (1 - a[3])).concat(1));
|
|
37
|
+
}
|
|
38
|
+
/** The same colour with a fixed alpha, written `#RRGGBBAA`. */
|
|
39
|
+
export function withAlpha(colour, alpha) {
|
|
40
|
+
const [r, g, b] = parseHex(colour);
|
|
41
|
+
return toHex([r, g, b, alpha]);
|
|
42
|
+
}
|
|
43
|
+
function luminance(colour) {
|
|
44
|
+
const [r, g, b] = parseHex(colour);
|
|
45
|
+
const linear = [r, g, b].map((value) => {
|
|
46
|
+
const x = value / 255;
|
|
47
|
+
return x <= 0.03928 ? x / 12.92 : ((x + 0.055) / 1.055) ** 2.4;
|
|
48
|
+
});
|
|
49
|
+
return 0.2126 * linear[0] + 0.7152 * linear[1] + 0.0722 * linear[2];
|
|
50
|
+
}
|
|
51
|
+
/** WCAG 2 contrast ratio between two opaque colours. */
|
|
52
|
+
export function contrastRatio(a, b) {
|
|
53
|
+
const x = luminance(a);
|
|
54
|
+
const y = luminance(b);
|
|
55
|
+
return (Math.max(x, y) + 0.05) / (Math.min(x, y) + 0.05);
|
|
56
|
+
}
|
|
57
|
+
function toHsl(colour) {
|
|
58
|
+
const [r, g, b] = parseHex(colour).slice(0, 3).map((x) => x / 255);
|
|
59
|
+
const max = Math.max(r, g, b);
|
|
60
|
+
const min = Math.min(r, g, b);
|
|
61
|
+
const l = (max + min) / 2;
|
|
62
|
+
if (max === min)
|
|
63
|
+
return [0, 0, l];
|
|
64
|
+
const d = max - min;
|
|
65
|
+
const s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
|
66
|
+
const h = max === r ? (g - b) / d + (g < b ? 6 : 0) : max === g ? (b - r) / d + 2 : (r - g) / d + 4;
|
|
67
|
+
return [h / 6, s, l];
|
|
68
|
+
}
|
|
69
|
+
function fromHsl(h, s, l) {
|
|
70
|
+
if (s === 0)
|
|
71
|
+
return toHex([l * 255, l * 255, l * 255, 1]);
|
|
72
|
+
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
|
73
|
+
const p = 2 * l - q;
|
|
74
|
+
const hue = (t) => {
|
|
75
|
+
const u = t < 0 ? t + 1 : t > 1 ? t - 1 : t;
|
|
76
|
+
if (u < 1 / 6)
|
|
77
|
+
return p + (q - p) * 6 * u;
|
|
78
|
+
if (u < 1 / 2)
|
|
79
|
+
return q;
|
|
80
|
+
if (u < 2 / 3)
|
|
81
|
+
return p + (q - p) * (2 / 3 - u) * 6;
|
|
82
|
+
return p;
|
|
83
|
+
};
|
|
84
|
+
return toHex([hue(h + 1 / 3) * 255, hue(h) * 255, hue(h - 1 / 3) * 255, 1]);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Darken (`direction` -1) or lighten (+1) `colour` in HSL lightness steps of 0.01, keeping hue and
|
|
88
|
+
* saturation, until it reaches `minimum` against every colour in `against`. Returns the first step
|
|
89
|
+
* that does, or the last step tried when lightness runs out.
|
|
90
|
+
*/
|
|
91
|
+
export function adjustForContrast(colour, against, minimum, direction) {
|
|
92
|
+
const [h, s, l] = toHsl(colour);
|
|
93
|
+
let candidate = colour;
|
|
94
|
+
for (let step = 0; step <= 100; step += 1) {
|
|
95
|
+
const lightness = Math.max(0, Math.min(1, l + direction * step * 0.01));
|
|
96
|
+
candidate = step === 0 ? colour : fromHsl(h, s, lightness);
|
|
97
|
+
if (against.every((other) => contrastRatio(candidate, other) >= minimum))
|
|
98
|
+
return candidate;
|
|
99
|
+
if (lightness === 0 || lightness === 1)
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
return candidate;
|
|
103
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Write a resolved appearance as the CSS custom properties the renderer stylesheet reads. The
|
|
3
|
+
* names are the ones the style packs have always set, so the components need no change; the
|
|
4
|
+
* values now come from the Host's appearance instead of a `data-pack` selector.
|
|
5
|
+
*/
|
|
6
|
+
import type { ResolvedAppearance } from "./resolve.ts";
|
|
7
|
+
/** How much text-sized parts (icons, control and row heights) grow: the ratio body text grows by. */
|
|
8
|
+
export declare function textSizedFactor(appearance: ResolvedAppearance): number;
|
|
9
|
+
/**
|
|
10
|
+
* The system font stacks the stylesheet defines on every surface. Elements outside a surface (the
|
|
11
|
+
* Host's own buttons) need them too, so `useLittlePressAppearance().cssVariables` includes them.
|
|
12
|
+
*/
|
|
13
|
+
export declare const systemFontStacks: Readonly<Record<string, string>>;
|
|
14
|
+
/** CSS custom properties for a surface root, keyed with their leading `--`. */
|
|
15
|
+
export declare function appearanceCssVariables(appearance: ResolvedAppearance): Record<string, string>;
|
|
16
|
+
/** Attributes the stylesheet selects on: control shapes, motion and the large-text layout. */
|
|
17
|
+
export declare function appearanceDataAttributes(appearance: ResolvedAppearance): Record<string, string>;
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Write a resolved appearance as the CSS custom properties the renderer stylesheet reads. The
|
|
3
|
+
* names are the ones the style packs have always set, so the components need no change; the
|
|
4
|
+
* values now come from the Host's appearance instead of a `data-pack` selector.
|
|
5
|
+
*/
|
|
6
|
+
import { scaleTextSize } from "./textScale.js";
|
|
7
|
+
const typeTokens = {
|
|
8
|
+
display: "display",
|
|
9
|
+
headline: "headline",
|
|
10
|
+
titleLarge: "title-large",
|
|
11
|
+
titleSmall: "title-small",
|
|
12
|
+
bodyLarge: "body-large",
|
|
13
|
+
bodyMedium: "body-medium",
|
|
14
|
+
bodySmall: "body-small",
|
|
15
|
+
labelLarge: "label-large",
|
|
16
|
+
labelSmall: "label-small",
|
|
17
|
+
};
|
|
18
|
+
const colourTokens = {
|
|
19
|
+
background: "background",
|
|
20
|
+
surface: "surface",
|
|
21
|
+
surfaceVariant: "surface-variant",
|
|
22
|
+
surfaceDisabled: "surface-disabled",
|
|
23
|
+
text: "text",
|
|
24
|
+
textVariant: "text-variant",
|
|
25
|
+
textDisabled: "text-disabled",
|
|
26
|
+
onAccent: "text-reverse",
|
|
27
|
+
onAccentVariant: "text-reverse-variant",
|
|
28
|
+
hover: "hover",
|
|
29
|
+
accent: "accent",
|
|
30
|
+
accentContainer: "accent-container",
|
|
31
|
+
outline: "outline",
|
|
32
|
+
positive: "pass",
|
|
33
|
+
warning: "caution",
|
|
34
|
+
danger: "error",
|
|
35
|
+
info: "extend-blue",
|
|
36
|
+
highlight: "extend-red",
|
|
37
|
+
series1: "series-1",
|
|
38
|
+
series2: "series-2",
|
|
39
|
+
series3: "series-3",
|
|
40
|
+
series4: "series-4",
|
|
41
|
+
};
|
|
42
|
+
const controlColourTokens = {
|
|
43
|
+
fill: "control-fill",
|
|
44
|
+
stroke: "control-stroke",
|
|
45
|
+
active: "control-active",
|
|
46
|
+
onActive: "control-on-active",
|
|
47
|
+
tabSurface: "control-tab-surface",
|
|
48
|
+
tabActiveSurface: "control-tab-active-surface",
|
|
49
|
+
tabText: "control-tab-text",
|
|
50
|
+
tabActiveText: "control-tab-active-text",
|
|
51
|
+
tabIndicator: "control-tab-indicator",
|
|
52
|
+
thumbFill: "control-thumb-fill",
|
|
53
|
+
};
|
|
54
|
+
/** Component-facing names and the theme variable each one reads. */
|
|
55
|
+
const aliases = [
|
|
56
|
+
["bg", "background"], ["surface-2", "surface-variant"], ["ink", "text"], ["ink-2", "text-variant"],
|
|
57
|
+
["muted", "text-variant"], ["hairline", "outline"], ["accent-ink", "text-reverse"], ["accent-soft", "accent-container"],
|
|
58
|
+
["ok", "pass"], ["warn", "caution"], ["live", "error"], ["team-a", "extend-red"], ["team-b", "extend-blue"],
|
|
59
|
+
["title", "headline"], ["heading", "title-small"], ["dek", "body-large"], ["body", "body-medium"],
|
|
60
|
+
["caption", "label-small"], ["overline", "label-small"], ["meta", "label-small"], ["stat", "display"],
|
|
61
|
+
["btn", "label-large"], ["radius-card", "radius-surface-lg"], ["radius-media", "radius-surface"],
|
|
62
|
+
["radius-btn", "radius-control"],
|
|
63
|
+
];
|
|
64
|
+
const px = (value) => `${Math.round(value * 100) / 100}px`;
|
|
65
|
+
function fontStack(font) {
|
|
66
|
+
const generic = font.generic === "serif" ? "var(--sys-serif)" : "var(--sys-sans)";
|
|
67
|
+
return font.name ? `'${font.name.replace(/['\\]/g, "")}', ${generic}` : generic;
|
|
68
|
+
}
|
|
69
|
+
function shadow(layers) {
|
|
70
|
+
if (layers.length === 0)
|
|
71
|
+
return "none";
|
|
72
|
+
return layers.map((layer) => `${px(layer.x)} ${px(layer.y)} ${px(layer.blur)} ${px(layer.spread)} ${layer.color}`).join(", ");
|
|
73
|
+
}
|
|
74
|
+
/** How much text-sized parts (icons, control and row heights) grow: the ratio body text grows by. */
|
|
75
|
+
export function textSizedFactor(appearance) {
|
|
76
|
+
const body = appearance.type.bodyMedium.size;
|
|
77
|
+
return scaleTextSize(body, appearance.textScale) / body;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* The system font stacks the stylesheet defines on every surface. Elements outside a surface (the
|
|
81
|
+
* Host's own buttons) need them too, so `useLittlePressAppearance().cssVariables` includes them.
|
|
82
|
+
*/
|
|
83
|
+
export const systemFontStacks = {
|
|
84
|
+
"--sys-sans": 'system-ui, -apple-system, "PingFang SC", "Noto Sans SC", sans-serif',
|
|
85
|
+
"--sys-serif": 'Georgia, "Songti SC", serif',
|
|
86
|
+
"--sys-mono": 'ui-monospace, "SF Mono", Menlo, monospace',
|
|
87
|
+
};
|
|
88
|
+
/** CSS custom properties for a surface root, keyed with their leading `--`. */
|
|
89
|
+
export function appearanceCssVariables(appearance) {
|
|
90
|
+
const variables = {};
|
|
91
|
+
const set = (name, value) => { variables[`--${name}`] = value; };
|
|
92
|
+
for (const [name, token] of Object.entries(colourTokens))
|
|
93
|
+
set(token, appearance.color[name]);
|
|
94
|
+
for (const [name, token] of Object.entries(controlColourTokens)) {
|
|
95
|
+
set(token, appearance.controlColor[name]);
|
|
96
|
+
}
|
|
97
|
+
set("shadow-card", shadow(appearance.shadow.card));
|
|
98
|
+
set("control-tab-shadow", shadow(appearance.shadow.controlTab));
|
|
99
|
+
set("font-display", fontStack(appearance.font.display));
|
|
100
|
+
set("font-text", fontStack(appearance.font.text));
|
|
101
|
+
for (const [role, token] of Object.entries(typeTokens)) {
|
|
102
|
+
const style = appearance.type[role];
|
|
103
|
+
set(token, `${style.weight} ${px(scaleTextSize(style.size, appearance.textScale))}/${style.lineHeight} var(--font-${style.font})`);
|
|
104
|
+
}
|
|
105
|
+
set("overline-tracking", `${appearance.tracking.overline}em`);
|
|
106
|
+
set("meta-tracking", `${appearance.tracking.meta}em`);
|
|
107
|
+
for (const [name, value] of Object.entries(appearance.space))
|
|
108
|
+
set(name === "blockGap" ? "block-gap" : `space-${name}`, px(value));
|
|
109
|
+
const grow = textSizedFactor(appearance);
|
|
110
|
+
set("size-control", px(appearance.size.control * grow));
|
|
111
|
+
set("size-row", px(appearance.size.row * grow));
|
|
112
|
+
set("size-avatar", px(appearance.size.avatar));
|
|
113
|
+
set("size-icon", px(appearance.size.icon * grow));
|
|
114
|
+
set("radius-control", px(appearance.radius.control));
|
|
115
|
+
set("radius-surface", px(appearance.radius.surface));
|
|
116
|
+
set("radius-surface-lg", px(appearance.radius.surfaceLarge));
|
|
117
|
+
set("radius-pill", px(appearance.radius.pill));
|
|
118
|
+
const controls = appearance.controls;
|
|
119
|
+
set("control-corner", "var(--radius-surface)");
|
|
120
|
+
set("control-mark-radius", px(controls.markRadius));
|
|
121
|
+
set("control-track-height", px(controls.trackHeight));
|
|
122
|
+
set("control-thumb-width", px(controls.thumbWidth * grow));
|
|
123
|
+
set("control-thumb-height", px(controls.thumbHeight * grow));
|
|
124
|
+
set("control-thumb-radius", px(controls.thumbRadius));
|
|
125
|
+
set("control-tab-radius", px(controls.tabRadius));
|
|
126
|
+
set("control-tab-track-padding", px(controls.tabTrackPadding));
|
|
127
|
+
set("control-tab-segment-radius", px(controls.tabSegmentRadius));
|
|
128
|
+
set("control-tab-padding-x", px(controls.tabPaddingX));
|
|
129
|
+
set("control-tab-gap", px(controls.tabGap));
|
|
130
|
+
set("control-tab-weight", String(controls.tabWeight));
|
|
131
|
+
set("control-tab-active-weight", String(controls.tabActiveWeight));
|
|
132
|
+
set("control-tab-grow", controls.tabsFullWidth ? "1" : "0");
|
|
133
|
+
// The same names every style pack defined, so a Host's own theme reaches every component.
|
|
134
|
+
for (const [name, target] of aliases)
|
|
135
|
+
set(name, `var(--${target})`);
|
|
136
|
+
set("lp-min-touch", px(appearance.minTouchTarget));
|
|
137
|
+
set("lp-text-scale", String(appearance.textScale));
|
|
138
|
+
// Fixed pixel sizes in the stylesheet (labels, tabs, code) grow by the same ratio as body text.
|
|
139
|
+
set("lp-text-zoom", String(Math.round(grow * 1000) / 1000));
|
|
140
|
+
return variables;
|
|
141
|
+
}
|
|
142
|
+
/** Attributes the stylesheet selects on: control shapes, motion and the large-text layout. */
|
|
143
|
+
export function appearanceDataAttributes(appearance) {
|
|
144
|
+
return {
|
|
145
|
+
"data-mode": appearance.mode,
|
|
146
|
+
"data-contrast": appearance.contrast,
|
|
147
|
+
"data-fields": appearance.controls.fields,
|
|
148
|
+
"data-tabs": appearance.controls.tabs,
|
|
149
|
+
"data-tabs-full": appearance.controls.tabsFullWidth ? "true" : "false",
|
|
150
|
+
"data-reduced-motion": appearance.reducedMotion ? "true" : "false",
|
|
151
|
+
"data-text-scale": appearance.textScale >= 1.5 ? "large" : "normal",
|
|
152
|
+
};
|
|
153
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Theme } from "./theme.ts";
|
|
2
|
+
export type ThemePresetId = "neutral" | "editorial" | "minimal" | "system" | "m3" | "companion";
|
|
3
|
+
/** Ready-made themes. Use one as it is, or copy it and change what you need. */
|
|
4
|
+
export declare const themePresets: Readonly<Record<ThemePresetId, Theme>>;
|