@tanqory/theme-kit 0.1.0 → 2.0.0-alpha.19
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 +21 -0
- package/README.md +128 -10
- package/dist/abi/canvas-live.d.ts +72 -0
- package/dist/abi/canvas-live.js +159 -0
- package/dist/abi/cart.d.ts +60 -0
- package/dist/abi/cart.js +396 -0
- package/dist/abi/convert.d.ts +360 -0
- package/dist/abi/convert.js +1330 -0
- package/dist/abi/data-api.d.ts +15 -0
- package/dist/abi/data-api.js +213 -0
- package/dist/abi/jcs.d.ts +6 -0
- package/dist/abi/jcs.js +28 -0
- package/dist/abi/labels.d.ts +3 -0
- package/dist/abi/labels.js +34 -0
- package/dist/abi/page-data.d.ts +264 -0
- package/dist/abi/page-data.js +746 -0
- package/dist/abi/page.d.ts +28 -0
- package/dist/abi/page.js +57 -0
- package/dist/abi/referrals.d.ts +23 -0
- package/dist/abi/referrals.js +89 -0
- package/dist/abi/rewards.d.ts +27 -0
- package/dist/abi/rewards.js +82 -0
- package/dist/abi/seo.d.ts +47 -0
- package/dist/abi/seo.js +68 -0
- package/dist/abi/shell.d.ts +23 -0
- package/dist/abi/shell.js +10 -0
- package/dist/abi/state-pack.d.ts +17 -0
- package/dist/abi/state-pack.js +43 -0
- package/dist/abi/wire.d.ts +46 -0
- package/dist/abi/wire.js +114 -0
- package/dist/build.d.ts +121 -0
- package/dist/build.js +401 -0
- package/dist/chunks.d.ts +23 -0
- package/dist/chunks.js +41 -0
- package/dist/client.d.ts +26 -0
- package/dist/client.js +101 -0
- package/dist/contract.d.ts +6 -0
- package/dist/contract.js +6 -0
- package/dist/headless/ai.d.ts +132 -0
- package/dist/headless/ai.js +99 -0
- package/dist/headless/hooks.d.ts +276 -0
- package/dist/headless/hooks.js +544 -0
- package/dist/headless/i18n.d.ts +13 -0
- package/dist/headless/i18n.js +45 -0
- package/dist/headless/index.d.ts +44 -0
- package/dist/headless/index.js +48 -0
- package/dist/headless/lazy-component.d.ts +72 -0
- package/dist/headless/lazy-component.js +225 -0
- package/dist/headless/lazy-section.d.ts +38 -0
- package/dist/headless/lazy-section.js +97 -0
- package/dist/headless/model.d.ts +73 -0
- package/dist/headless/model.js +115 -0
- package/dist/headless/money.d.ts +17 -0
- package/dist/headless/money.js +63 -0
- package/dist/headless/page.d.ts +45 -0
- package/dist/headless/page.js +53 -0
- package/dist/headless/platform.d.ts +55 -0
- package/dist/headless/platform.js +77 -0
- package/dist/headless/primitives/cart.d.ts +90 -0
- package/dist/headless/primitives/cart.js +153 -0
- package/dist/headless/primitives/collection.d.ts +68 -0
- package/dist/headless/primitives/collection.js +80 -0
- package/dist/headless/primitives/product.d.ts +101 -0
- package/dist/headless/primitives/product.js +117 -0
- package/dist/headless/primitives/storefront.d.ts +106 -0
- package/dist/headless/primitives/storefront.js +187 -0
- package/dist/headless/primitives/util.d.ts +19 -0
- package/dist/headless/primitives/util.js +42 -0
- package/dist/headless/strings.d.ts +9 -0
- package/dist/headless/strings.js +198 -0
- package/dist/headless/structured-data.d.ts +31 -0
- package/dist/headless/structured-data.js +57 -0
- package/dist/headless/tanqory-money.generated.d.ts +493 -0
- package/dist/headless/tanqory-money.generated.js +507 -0
- package/dist/headless/theme.d.ts +109 -0
- package/dist/headless/theme.js +62 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +21 -553
- package/dist/runtime/data/StoreProvider.d.ts +84 -0
- package/dist/runtime/data/StoreProvider.js +380 -0
- package/dist/runtime/data/handle.d.ts +14 -0
- package/dist/runtime/data/handle.js +16 -0
- package/dist/runtime/data/routes.d.ts +87 -0
- package/dist/runtime/data/routes.js +158 -0
- package/dist/runtime/data/to-ui.d.ts +286 -0
- package/dist/runtime/data/to-ui.js +1009 -0
- package/dist/runtime/entries/register.d.ts +9 -0
- package/dist/runtime/entries/register.js +9 -0
- package/dist/runtime/entries/sections.d.ts +16 -0
- package/dist/runtime/entries/sections.js +19 -0
- package/dist/{types → runtime/kit}/SectionTree.d.ts +2 -1
- package/dist/runtime/kit/SectionTree.js +100 -0
- package/dist/runtime/kit/analytics.d.ts +63 -0
- package/dist/runtime/kit/analytics.js +250 -0
- package/dist/runtime/kit/cart.d.ts +222 -0
- package/dist/runtime/kit/cart.js +545 -0
- package/dist/{types → runtime/kit}/composition.d.ts +1 -1
- package/dist/runtime/kit/composition.js +3 -0
- package/dist/{types → runtime/kit}/config.d.ts +2 -2
- package/dist/runtime/kit/config.js +6 -0
- package/dist/runtime/kit/consent.d.ts +29 -0
- package/dist/runtime/kit/consent.js +159 -0
- package/dist/runtime/kit/contract/content.d.ts +213 -0
- package/dist/runtime/kit/contract/content.js +119 -0
- package/dist/runtime/kit/contract/field-types.d.ts +360 -0
- package/dist/runtime/kit/contract/field-types.js +216 -0
- package/dist/runtime/kit/contract/groups.d.ts +230 -0
- package/dist/runtime/kit/contract/groups.js +402 -0
- package/dist/runtime/kit/contract/index.d.ts +33 -0
- package/dist/runtime/kit/contract/index.js +33 -0
- package/dist/runtime/kit/contract/transform.d.ts +82 -0
- package/dist/runtime/kit/contract/transform.js +133 -0
- package/dist/runtime/kit/contract/validate.d.ts +77 -0
- package/dist/runtime/kit/contract/validate.js +520 -0
- package/dist/runtime/kit/data.d.ts +732 -0
- package/dist/runtime/kit/data.js +1131 -0
- package/dist/{types → runtime/kit}/defineSection.d.ts +1 -1
- package/dist/runtime/kit/defineSection.js +10 -0
- package/dist/runtime/kit/dynamic-source.d.ts +118 -0
- package/dist/runtime/kit/dynamic-source.js +311 -0
- package/dist/runtime/kit/index.d.ts +24 -0
- package/dist/runtime/kit/index.js +24 -0
- package/dist/{types → runtime/kit}/jsx-to-json.d.ts +1 -1
- package/dist/runtime/kit/jsx-to-json.js +28 -0
- package/dist/{types → runtime/kit}/mount.d.ts +25 -4
- package/dist/runtime/kit/mount.js +124 -0
- package/dist/{types → runtime/kit}/registry.d.ts +1 -1
- package/dist/runtime/kit/registry.js +12 -0
- package/dist/{types → runtime/kit}/ssg.d.ts +9 -3
- package/dist/runtime/kit/ssg.js +56 -0
- package/dist/runtime/kit/storefront.d.ts +652 -0
- package/dist/runtime/kit/storefront.js +723 -0
- package/dist/{types → runtime/kit}/theme-context.d.ts +8 -2
- package/dist/runtime/kit/theme-context.js +25 -0
- package/dist/runtime/kit/types.d.ts +155 -0
- package/dist/runtime/kit/types.js +1 -0
- package/dist/runtime/platform/TrackingPixels.d.ts +14 -0
- package/dist/runtime/platform/TrackingPixels.js +63 -0
- package/dist/runtime/platform/environment.d.ts +68 -0
- package/dist/runtime/platform/environment.js +85 -0
- package/dist/runtime/platform/locale.d.ts +27 -0
- package/dist/runtime/platform/locale.js +42 -0
- package/dist/runtime/platform/route-analytics.d.ts +15 -0
- package/dist/runtime/platform/route-analytics.js +72 -0
- package/dist/runtime/platform/safe-href.d.ts +25 -0
- package/dist/runtime/platform/safe-href.js +69 -0
- package/dist/runtime/platform/safe-values.d.ts +37 -0
- package/dist/runtime/platform/safe-values.js +140 -0
- package/dist/runtime/render/SectionRenderer.d.ts +4 -0
- package/dist/runtime/render/SectionRenderer.js +199 -0
- package/dist/runtime/render/listing-query.d.ts +52 -0
- package/dist/runtime/render/listing-query.js +122 -0
- package/dist/runtime/render/localized-copy.d.ts +86 -0
- package/dist/runtime/render/localized-copy.js +129 -0
- package/dist/runtime/render/product-listing.d.ts +11 -0
- package/dist/runtime/render/product-listing.js +243 -0
- package/dist/runtime/render/resolved.d.ts +47 -0
- package/dist/runtime/render/resolved.js +80 -0
- package/dist/runtime/render/section-actions.d.ts +67 -0
- package/dist/runtime/render/section-actions.js +205 -0
- package/dist/runtime/render/starter.d.ts +11 -0
- package/dist/runtime/render/starter.js +96 -0
- package/dist/runtime/seo/head.d.ts +115 -0
- package/dist/runtime/seo/head.js +281 -0
- package/dist/runtime/seo/structured-data.d.ts +83 -0
- package/dist/runtime/seo/structured-data.js +252 -0
- package/dist/runtime/shell/PageShell.d.ts +18 -0
- package/dist/runtime/shell/PageShell.js +118 -0
- package/dist/runtime/shell/RuntimeBridge.d.ts +18 -0
- package/dist/runtime/shell/RuntimeBridge.js +124 -0
- package/dist/runtime/shell/overlay-state.d.ts +31 -0
- package/dist/runtime/shell/overlay-state.js +59 -0
- package/dist/runtime/shell/preview-mode.d.ts +2 -0
- package/dist/runtime/shell/preview-mode.js +11 -0
- package/dist/runtime/shell/spa-router.d.ts +14 -0
- package/dist/runtime/shell/spa-router.js +76 -0
- package/dist/runtime/shell/url-redirects.d.ts +6 -0
- package/dist/runtime/shell/url-redirects.js +37 -0
- package/dist/runtime/slots.d.ts +74 -0
- package/dist/runtime/slots.js +16 -0
- package/dist/runtime.d.ts +34 -0
- package/dist/runtime.js +40 -0
- package/dist/sdk/app-block.d.ts +29 -0
- package/dist/sdk/app-block.js +22 -0
- package/dist/sdk/font-hooks.d.ts +7 -0
- package/dist/sdk/font-hooks.js +21 -0
- package/dist/sdk/fonts.d.ts +144 -0
- package/dist/sdk/fonts.js +241 -0
- package/dist/sdk/index.d.ts +32 -0
- package/dist/sdk/index.js +36 -0
- package/dist/sdk/motion-hooks.d.ts +23 -0
- package/dist/sdk/motion-hooks.js +46 -0
- package/dist/sdk/motion.d.ts +78 -0
- package/dist/sdk/motion.js +292 -0
- package/dist/sdk/referrals.d.ts +62 -0
- package/dist/sdk/referrals.js +20 -0
- package/dist/sdk/rewards.d.ts +85 -0
- package/dist/sdk/rewards.js +29 -0
- package/dist/sdk/runtime-context.d.ts +230 -0
- package/dist/sdk/runtime-context.js +73 -0
- package/dist/sdk/schema.d.ts +249 -0
- package/dist/sdk/schema.js +168 -0
- package/dist/sdk/server-render.d.ts +8 -0
- package/dist/sdk/server-render.js +12 -0
- package/dist/sdk/store-lookup.d.ts +32 -0
- package/dist/sdk/store-lookup.js +48 -0
- package/dist/sdk/store.d.ts +164 -0
- package/dist/sdk/store.js +5 -0
- package/dist/sdk/sx.d.ts +45 -0
- package/dist/sdk/sx.js +117 -0
- package/dist/sdk/theme.d.ts +212 -0
- package/dist/sdk/theme.js +27 -0
- package/dist/sdk/types.d.ts +789 -0
- package/dist/sdk/types.js +7 -0
- package/dist/server.d.ts +112 -0
- package/dist/server.js +589 -0
- package/dist/vite-chunks.d.ts +10 -0
- package/dist/vite-chunks.js +44 -0
- package/docs/headless-api.md +250 -0
- package/package.json +72 -26
- package/dist/index.js.map +0 -1
- package/dist/ssg-BZ5FYTw7.js +0 -1802
- package/dist/ssg-BZ5FYTw7.js.map +0 -1
- package/dist/ssg.js +0 -8
- package/dist/ssg.js.map +0 -1
- package/dist/types/cart.d.ts +0 -86
- package/dist/types/data.d.ts +0 -357
- package/dist/types/dynamic-source.d.ts +0 -84
- package/dist/types/editor.d.ts +0 -5
- package/dist/types/index.d.ts +0 -20
- package/dist/types/preview-bridge.d.ts +0 -9
- package/dist/types/storefront.d.ts +0 -333
- package/dist/types/types.d.ts +0 -44
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React hooks over `motion.ts`: each runs in an effect only (never during the server render, so nothing of it is in the
|
|
3
|
+
* server HTML), and undoes itself when the component unmounts or its inputs change.
|
|
4
|
+
*
|
|
5
|
+
* const ref = useRef<HTMLDivElement>(null)
|
|
6
|
+
* useReveal(ref, { preset: 'fade-up' })
|
|
7
|
+
* useScrollProgress(ref, { cssVar: '--tq-progress' }) // CSS: transform: translateY(calc(var(--tq-progress) * -40px))
|
|
8
|
+
* useCssVars(ref, { '--drag-x': x }) // a value that changes every frame
|
|
9
|
+
*/
|
|
10
|
+
import { useEffect, useState } from 'react';
|
|
11
|
+
import { onReducedMotionChange, prefersReducedMotion, reveal, scrollProgress, setCssVars } from './motion.js';
|
|
12
|
+
/** Custom properties of the element, written after each render that changes them (setCssVars). */
|
|
13
|
+
export function useCssVars(ref, vars) {
|
|
14
|
+
const key = JSON.stringify(vars);
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
setCssVars(ref.current, vars);
|
|
17
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
18
|
+
}, [ref, key]);
|
|
19
|
+
}
|
|
20
|
+
/** The shopper's reduced-motion setting, following changes; false on the server and on the first render. */
|
|
21
|
+
export function useReducedMotion() {
|
|
22
|
+
const [reduce, setReduce] = useState(false);
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
setReduce(prefersReducedMotion());
|
|
25
|
+
return onReducedMotionChange(setReduce);
|
|
26
|
+
}, []);
|
|
27
|
+
return reduce;
|
|
28
|
+
}
|
|
29
|
+
/** The element fades or slides in when it comes on screen (reveal). */
|
|
30
|
+
export function useReveal(ref, o = {}) {
|
|
31
|
+
const key = JSON.stringify(o);
|
|
32
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
33
|
+
useEffect(() => reveal(ref.current, o), [ref, key]);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* How far the element has travelled through the screen (0 to 1, scrollProgress). With `cssVar` the value is written to
|
|
37
|
+
* that custom property and the hook answers nothing that re-renders (the cheap way: CSS reads the variable); without it,
|
|
38
|
+
* the value is React state, updated at most once a frame.
|
|
39
|
+
*/
|
|
40
|
+
export function useScrollProgress(ref, o = {}) {
|
|
41
|
+
const [p, setP] = useState(null);
|
|
42
|
+
const key = JSON.stringify(o);
|
|
43
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
44
|
+
useEffect(() => scrollProgress(ref.current, o.cssVar ? null : setP, o), [ref, key]);
|
|
45
|
+
return o.cssVar ? null : p;
|
|
46
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Continuous values and motion for themes, under the platform CSP (theme-abi §5: `style-src-attr 'none'`, no
|
|
3
|
+
* 'unsafe-inline', script CSP unchanged).
|
|
4
|
+
*
|
|
5
|
+
* CSS VARIABLES. A value that changes with every scroll or drag frame cannot be a class (`sx()` makes one class per
|
|
6
|
+
* value). `setCssVars(el, { '--progress': 0.42 })` writes custom properties through the CSSOM
|
|
7
|
+
* (`CSSStyleDeclaration.setProperty`). CSP governs style *attributes* the parser reads from markup and `<style>`
|
|
8
|
+
* elements; it does not govern the CSSOM: CSP Level 2 §7.16 and CSP Level 3 §6.1.15 (`style-src-attr` "governs the
|
|
9
|
+
* behaviour of style attributes") restrict a style attribute the user agent would apply from the document, and the only
|
|
10
|
+
* CSSOM gating either spec names is `cssText` setters and `insertRule` under `'unsafe-eval'` (CSP3 §6.1.13 item 4,
|
|
11
|
+
* CSP2 §7.16; an editor's note there, which browsers do not apply). `setProperty` is neither, and this module never
|
|
12
|
+
* uses `cssText`, `insertRule` or `setAttribute('style')`.
|
|
13
|
+
* Checked in Chromium 148 and WebKit 26.4 under the storefront's own policy (2026-11-19): `setProperty('--w', '42px')`
|
|
14
|
+
* applies; a `style` attribute in markup or set with `setAttribute` is refused. A variable is never in the server HTML
|
|
15
|
+
* (where it would be a refused attribute): only browser code calls this, and only names that start with `--`.
|
|
16
|
+
*
|
|
17
|
+
* MOTION. `reveal()` (an element entering the screen) and `exit()` (leaving, before it is removed) run on the Web
|
|
18
|
+
* Animations API (`Element.animate`), which writes no attribute at all; `scrollProgress()` reports how far an element
|
|
19
|
+
* has travelled through the screen, for scroll-linked effects, and can write it to a variable. Everything honours
|
|
20
|
+
* `prefers-reduced-motion: reduce` (Media Queries Level 5 §11.6): no movement, only the end state (a reveal shows at
|
|
21
|
+
* once, an exit removes at once), and scroll-linked values still report (the theme decides what they do) unless the
|
|
22
|
+
* caller asks for `staticWhenReduced`. Every function is a no-op on the server.
|
|
23
|
+
*/
|
|
24
|
+
/** The string a variable is set to, or null when the value is refused (valueScan). */
|
|
25
|
+
export declare function cssVarValue(value: unknown): string | null;
|
|
26
|
+
export type CssVars = Readonly<Record<string, number | string | null | undefined>>;
|
|
27
|
+
/**
|
|
28
|
+
* Set (or, with null / undefined, remove) custom properties on an element, in one frame. A name that is not a custom
|
|
29
|
+
* property or a value the grammar refuses is skipped, never escaped into place. Answers the names written.
|
|
30
|
+
*/
|
|
31
|
+
export declare function setCssVars(el: Element | null | undefined, vars: CssVars): string[];
|
|
32
|
+
/** The shopper asked for less motion (their system setting). false on the server. */
|
|
33
|
+
export declare function prefersReducedMotion(): boolean;
|
|
34
|
+
/** Calls `cb` when the setting changes; answers the function that stops it. */
|
|
35
|
+
export declare function onReducedMotionChange(cb: (reduce: boolean) => void): () => void;
|
|
36
|
+
export type MotionPreset = 'fade' | 'fade-up' | 'fade-down' | 'slide-left' | 'slide-right' | 'scale';
|
|
37
|
+
export interface MotionOptions {
|
|
38
|
+
preset?: MotionPreset;
|
|
39
|
+
/** ms, 0 to 2000. Default 500 (a reveal) and 250 (an exit). */
|
|
40
|
+
duration?: number;
|
|
41
|
+
/** ms, 0 to 2000. */
|
|
42
|
+
delay?: number;
|
|
43
|
+
/** A CSS easing; default the platform's `cubic-bezier(.2,.7,.2,1)`. */
|
|
44
|
+
easing?: string;
|
|
45
|
+
/** How far a slide or fade-up moves, in px (0 to 200). Default 24. */
|
|
46
|
+
distance?: number;
|
|
47
|
+
}
|
|
48
|
+
export interface RevealOptions extends MotionOptions {
|
|
49
|
+
/** The part of the element that must be on screen, 0 to 1. Default 0.15. */
|
|
50
|
+
threshold?: number;
|
|
51
|
+
/** Reveal once (default), or every time it comes back on screen. */
|
|
52
|
+
once?: boolean;
|
|
53
|
+
}
|
|
54
|
+
/** The keyframes of a preset, from hidden to shown. */
|
|
55
|
+
export declare function presetFrames(preset?: MotionPreset, distance?: number): Keyframe[];
|
|
56
|
+
/**
|
|
57
|
+
* Fades or slides an element in when it comes on screen (IntersectionObserver), with the Web Animations API. Reduced
|
|
58
|
+
* motion: it is shown at once. Answers the function that stops watching. An element that is never on screen stays as
|
|
59
|
+
* the theme's CSS draws it (the animation's first frame is applied only when it runs), so no content is hidden for good.
|
|
60
|
+
*/
|
|
61
|
+
export declare function reveal(el: Element | null | undefined, o?: RevealOptions): () => void;
|
|
62
|
+
/**
|
|
63
|
+
* Plays an element's exit (the preset backwards) and resolves when it is done, so the caller can remove it then (a
|
|
64
|
+
* drawer, a toast, a dismissed card). Reduced motion or no Web Animations: resolves at once.
|
|
65
|
+
*/
|
|
66
|
+
export declare function exit(el: Element | null | undefined, o?: MotionOptions): Promise<void>;
|
|
67
|
+
export interface ScrollProgressOptions {
|
|
68
|
+
/** Also write the progress (0 to 1, 4 decimals) to this custom property of the element, e.g. `--tq-progress`. */
|
|
69
|
+
cssVar?: string;
|
|
70
|
+
/** With reduced motion: report 1 once and stop (the end state), instead of following the scroll. */
|
|
71
|
+
staticWhenReduced?: boolean;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* How far an element has travelled through the screen: 0 when its top meets the bottom of the screen, 1 when its bottom
|
|
75
|
+
* leaves the top (the same range as CSS `view()` timelines, Scroll-driven Animations §3.2). Computed at most once a
|
|
76
|
+
* frame, only while the element is near the screen. Answers the function that stops it.
|
|
77
|
+
*/
|
|
78
|
+
export declare function scrollProgress(el: Element | null | undefined, cb: ((progress: number) => void) | null, o?: ScrollProgressOptions): () => void;
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Continuous values and motion for themes, under the platform CSP (theme-abi §5: `style-src-attr 'none'`, no
|
|
3
|
+
* 'unsafe-inline', script CSP unchanged).
|
|
4
|
+
*
|
|
5
|
+
* CSS VARIABLES. A value that changes with every scroll or drag frame cannot be a class (`sx()` makes one class per
|
|
6
|
+
* value). `setCssVars(el, { '--progress': 0.42 })` writes custom properties through the CSSOM
|
|
7
|
+
* (`CSSStyleDeclaration.setProperty`). CSP governs style *attributes* the parser reads from markup and `<style>`
|
|
8
|
+
* elements; it does not govern the CSSOM: CSP Level 2 §7.16 and CSP Level 3 §6.1.15 (`style-src-attr` "governs the
|
|
9
|
+
* behaviour of style attributes") restrict a style attribute the user agent would apply from the document, and the only
|
|
10
|
+
* CSSOM gating either spec names is `cssText` setters and `insertRule` under `'unsafe-eval'` (CSP3 §6.1.13 item 4,
|
|
11
|
+
* CSP2 §7.16; an editor's note there, which browsers do not apply). `setProperty` is neither, and this module never
|
|
12
|
+
* uses `cssText`, `insertRule` or `setAttribute('style')`.
|
|
13
|
+
* Checked in Chromium 148 and WebKit 26.4 under the storefront's own policy (2026-11-19): `setProperty('--w', '42px')`
|
|
14
|
+
* applies; a `style` attribute in markup or set with `setAttribute` is refused. A variable is never in the server HTML
|
|
15
|
+
* (where it would be a refused attribute): only browser code calls this, and only names that start with `--`.
|
|
16
|
+
*
|
|
17
|
+
* MOTION. `reveal()` (an element entering the screen) and `exit()` (leaving, before it is removed) run on the Web
|
|
18
|
+
* Animations API (`Element.animate`), which writes no attribute at all; `scrollProgress()` reports how far an element
|
|
19
|
+
* has travelled through the screen, for scroll-linked effects, and can write it to a variable. Everything honours
|
|
20
|
+
* `prefers-reduced-motion: reduce` (Media Queries Level 5 §11.6): no movement, only the end state (a reveal shows at
|
|
21
|
+
* once, an exit removes at once), and scroll-linked values still report (the theme decides what they do) unless the
|
|
22
|
+
* caller asks for `staticWhenReduced`. Every function is a no-op on the server.
|
|
23
|
+
*/
|
|
24
|
+
/** A custom property name: two hyphens, then letters, digits, `-` and `_` (CSS Variables Level 1 §2 `<custom-property-name>`). */
|
|
25
|
+
const VAR_NAME = /^--[A-Za-z_][A-Za-z0-9_-]{0,63}$/;
|
|
26
|
+
/**
|
|
27
|
+
* The values a variable may hold: numbers with an optional unit, percentages, hex colours, keywords, other custom
|
|
28
|
+
* property names, operators (`+ - * / ,`) and the functions rgb()/rgba()/hsl()/hsla()/calc()/min()/max()/clamp()/var()
|
|
29
|
+
* over those, nested two levels at most. Nothing that could load a resource (`url(`, `image-set(`, `src(`), close a
|
|
30
|
+
* declaration or a rule, or hide in an escape. Read by a single left-to-right scan (linear in the length, never a
|
|
31
|
+
* backtracking pattern: a regex over runs of atoms backtracked exponentially on `aaaa…!`, security review 2026-11-19).
|
|
32
|
+
*/
|
|
33
|
+
const FUNCS = new Set(['rgb', 'rgba', 'hsl', 'hsla', 'calc', 'min', 'max', 'clamp', 'var']);
|
|
34
|
+
const UNITS = new Set(['', '%', 'px', 'em', 'rem', 'ch', 'ex', 'vh', 'vw', 'vmin', 'vmax', 'svh', 'lvh', 'dvh', 'svw', 'lvw', 'dvw', 'cqw', 'cqh', 'deg', 'rad', 'turn', 's', 'ms', 'fr', 'x']);
|
|
35
|
+
const VALUE_MAX = 200;
|
|
36
|
+
const isDigit = (c) => c >= '0' && c <= '9';
|
|
37
|
+
const isAlpha = (c) => (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
|
|
38
|
+
const isHex = (c) => isDigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
|
|
39
|
+
/** One scan of `v`: true when every character belongs to an allowed token and the brackets close. */
|
|
40
|
+
function valueScan(v) {
|
|
41
|
+
let i = 0;
|
|
42
|
+
let depth = 0;
|
|
43
|
+
let tokens = 0;
|
|
44
|
+
while (i < v.length) {
|
|
45
|
+
const c = v[i];
|
|
46
|
+
if (c === ' ' || c === '\t') {
|
|
47
|
+
i++;
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
if (c === ')') {
|
|
51
|
+
if (depth === 0)
|
|
52
|
+
return false;
|
|
53
|
+
depth--;
|
|
54
|
+
i++;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (c === '+' || c === '*' || c === '/' || c === ',') {
|
|
58
|
+
i++;
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (c === '-' && !(isDigit(v[i + 1] ?? '') || v[i + 1] === '.' || v[i + 1] === '-' || isAlpha(v[i + 1] ?? ''))) {
|
|
62
|
+
i++;
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (c === '#') {
|
|
66
|
+
let j = i + 1;
|
|
67
|
+
while (j < v.length && isHex(v[j]))
|
|
68
|
+
j++;
|
|
69
|
+
if (j - i - 1 < 3 || j - i - 1 > 8)
|
|
70
|
+
return false;
|
|
71
|
+
i = j;
|
|
72
|
+
tokens++;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (isDigit(c) || c === '.' || (c === '-' && (isDigit(v[i + 1] ?? '') || v[i + 1] === '.'))) {
|
|
76
|
+
let j = c === '-' ? i + 1 : i;
|
|
77
|
+
let digits = 0;
|
|
78
|
+
while (j < v.length && isDigit(v[j])) {
|
|
79
|
+
j++;
|
|
80
|
+
digits++;
|
|
81
|
+
}
|
|
82
|
+
if (v[j] === '.') {
|
|
83
|
+
j++;
|
|
84
|
+
while (j < v.length && isDigit(v[j])) {
|
|
85
|
+
j++;
|
|
86
|
+
digits++;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (digits === 0)
|
|
90
|
+
return false;
|
|
91
|
+
if ((v[j] === 'e' || v[j] === 'E') && (isDigit(v[j + 1] ?? '') || (v[j + 1] === '-' && isDigit(v[j + 2] ?? '')))) {
|
|
92
|
+
j += v[j + 1] === '-' ? 2 : 1;
|
|
93
|
+
while (j < v.length && isDigit(v[j]))
|
|
94
|
+
j++;
|
|
95
|
+
}
|
|
96
|
+
let u = j;
|
|
97
|
+
while (u < v.length && (isAlpha(v[u]) || v[u] === '%'))
|
|
98
|
+
u++;
|
|
99
|
+
if (!UNITS.has(v.slice(j, u).toLowerCase()))
|
|
100
|
+
return false;
|
|
101
|
+
i = u;
|
|
102
|
+
tokens++;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (isAlpha(c) || c === '-') {
|
|
106
|
+
// a keyword, a custom property name (`--a`), or a function name followed by `(`
|
|
107
|
+
let j = i;
|
|
108
|
+
while (j < v.length && (isAlpha(v[j]) || isDigit(v[j]) || v[j] === '-' || v[j] === '_'))
|
|
109
|
+
j++;
|
|
110
|
+
const word = v.slice(i, j);
|
|
111
|
+
// a keyword is at most 32 characters, a custom property name 2 + 64 (VAR_NAME)
|
|
112
|
+
if (word.length > (word.startsWith('--') ? 66 : 32))
|
|
113
|
+
return false;
|
|
114
|
+
if (v[j] === '(') {
|
|
115
|
+
if (!FUNCS.has(word.toLowerCase()) || depth >= 2)
|
|
116
|
+
return false;
|
|
117
|
+
depth++;
|
|
118
|
+
i = j + 1;
|
|
119
|
+
tokens++;
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
if (word.startsWith('-') && !word.startsWith('--'))
|
|
123
|
+
return false;
|
|
124
|
+
i = j;
|
|
125
|
+
tokens++;
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
return depth === 0 && tokens > 0;
|
|
131
|
+
}
|
|
132
|
+
/** The string a variable is set to, or null when the value is refused (valueScan). */
|
|
133
|
+
export function cssVarValue(value) {
|
|
134
|
+
if (typeof value === 'number')
|
|
135
|
+
return Number.isFinite(value) ? String(Math.round(value * 1e4) / 1e4) : null;
|
|
136
|
+
if (typeof value !== 'string')
|
|
137
|
+
return null;
|
|
138
|
+
const v = value.trim();
|
|
139
|
+
if (!v || v.length > VALUE_MAX || /[;{}<>\\"'`]|url\(|image-set\(|src\(|\/\*/i.test(v))
|
|
140
|
+
return null;
|
|
141
|
+
return valueScan(v) ? v : null;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Set (or, with null / undefined, remove) custom properties on an element, in one frame. A name that is not a custom
|
|
145
|
+
* property or a value the grammar refuses is skipped, never escaped into place. Answers the names written.
|
|
146
|
+
*/
|
|
147
|
+
export function setCssVars(el, vars) {
|
|
148
|
+
const style = el?.style;
|
|
149
|
+
if (!style || typeof style.setProperty !== 'function')
|
|
150
|
+
return [];
|
|
151
|
+
const done = [];
|
|
152
|
+
for (const [name, raw] of Object.entries(vars)) {
|
|
153
|
+
if (!VAR_NAME.test(name))
|
|
154
|
+
continue;
|
|
155
|
+
if (raw === null || raw === undefined) {
|
|
156
|
+
style.removeProperty(name);
|
|
157
|
+
done.push(name);
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
const v = cssVarValue(raw);
|
|
161
|
+
if (v === null)
|
|
162
|
+
continue;
|
|
163
|
+
if (style.getPropertyValue(name) !== v)
|
|
164
|
+
style.setProperty(name, v);
|
|
165
|
+
done.push(name);
|
|
166
|
+
}
|
|
167
|
+
return done;
|
|
168
|
+
}
|
|
169
|
+
// ── reduced motion ─────────────────────────────────────────────────────────────────────────────────────────────
|
|
170
|
+
const REDUCE = '(prefers-reduced-motion: reduce)';
|
|
171
|
+
const hasWindow = () => typeof window !== 'undefined' && typeof window.matchMedia === 'function';
|
|
172
|
+
/** The shopper asked for less motion (their system setting). false on the server. */
|
|
173
|
+
export function prefersReducedMotion() {
|
|
174
|
+
return hasWindow() ? window.matchMedia(REDUCE).matches : false;
|
|
175
|
+
}
|
|
176
|
+
/** Calls `cb` when the setting changes; answers the function that stops it. */
|
|
177
|
+
export function onReducedMotionChange(cb) {
|
|
178
|
+
if (!hasWindow())
|
|
179
|
+
return () => { };
|
|
180
|
+
const mq = window.matchMedia(REDUCE);
|
|
181
|
+
const h = (e) => cb(e.matches);
|
|
182
|
+
mq.addEventListener?.('change', h);
|
|
183
|
+
return () => mq.removeEventListener?.('change', h);
|
|
184
|
+
}
|
|
185
|
+
const clamp = (n, lo, hi, d) => (typeof n === 'number' && Number.isFinite(n) ? Math.min(hi, Math.max(lo, n)) : d);
|
|
186
|
+
const EASING = /^(?:linear|ease(?:-in|-out|-in-out)?|cubic-bezier\((?:\s*-?\d*\.?\d+\s*,){3}\s*-?\d*\.?\d+\s*\)|steps\(\s*\d{1,3}\s*(?:,\s*(?:start|end|jump-(?:start|end|none|both))\s*)?\))$/;
|
|
187
|
+
/** The keyframes of a preset, from hidden to shown. */
|
|
188
|
+
export function presetFrames(preset = 'fade-up', distance = 24) {
|
|
189
|
+
const d = `${clamp(distance, 0, 200, 24)}px`;
|
|
190
|
+
const from = preset === 'fade' ? { opacity: 0 }
|
|
191
|
+
: preset === 'fade-down' ? { opacity: 0, transform: `translateY(-${d})` }
|
|
192
|
+
: preset === 'slide-left' ? { opacity: 0, transform: `translateX(${d})` }
|
|
193
|
+
: preset === 'slide-right' ? { opacity: 0, transform: `translateX(-${d})` }
|
|
194
|
+
: preset === 'scale' ? { opacity: 0, transform: 'scale(0.96)' }
|
|
195
|
+
: { opacity: 0, transform: `translateY(${d})` };
|
|
196
|
+
return [from, { opacity: 1, transform: 'none' }];
|
|
197
|
+
}
|
|
198
|
+
function timing(o, defDuration) {
|
|
199
|
+
return {
|
|
200
|
+
duration: clamp(o.duration, 0, 2000, defDuration),
|
|
201
|
+
delay: clamp(o.delay, 0, 2000, 0),
|
|
202
|
+
easing: typeof o.easing === 'string' && o.easing.length <= 64 && EASING.test(o.easing.trim()) ? o.easing.trim() : 'cubic-bezier(.2,.7,.2,1)',
|
|
203
|
+
fill: 'both',
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
const canAnimate = (el) => !!el && typeof el.animate === 'function';
|
|
207
|
+
/**
|
|
208
|
+
* Fades or slides an element in when it comes on screen (IntersectionObserver), with the Web Animations API. Reduced
|
|
209
|
+
* motion: it is shown at once. Answers the function that stops watching. An element that is never on screen stays as
|
|
210
|
+
* the theme's CSS draws it (the animation's first frame is applied only when it runs), so no content is hidden for good.
|
|
211
|
+
*/
|
|
212
|
+
export function reveal(el, o = {}) {
|
|
213
|
+
if (!canAnimate(el) || typeof IntersectionObserver === 'undefined')
|
|
214
|
+
return () => { };
|
|
215
|
+
const once = o.once !== false;
|
|
216
|
+
let running = null;
|
|
217
|
+
const io = new IntersectionObserver((entries) => {
|
|
218
|
+
for (const e of entries) {
|
|
219
|
+
if (!e.isIntersecting)
|
|
220
|
+
continue;
|
|
221
|
+
if (!prefersReducedMotion()) {
|
|
222
|
+
running?.cancel();
|
|
223
|
+
running = el.animate(presetFrames(o.preset, o.distance), timing(o, 500));
|
|
224
|
+
}
|
|
225
|
+
if (once)
|
|
226
|
+
io.disconnect();
|
|
227
|
+
}
|
|
228
|
+
}, { threshold: clamp(o.threshold, 0, 1, 0.15) });
|
|
229
|
+
io.observe(el);
|
|
230
|
+
return () => {
|
|
231
|
+
io.disconnect();
|
|
232
|
+
running?.cancel();
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Plays an element's exit (the preset backwards) and resolves when it is done, so the caller can remove it then (a
|
|
237
|
+
* drawer, a toast, a dismissed card). Reduced motion or no Web Animations: resolves at once.
|
|
238
|
+
*/
|
|
239
|
+
export function exit(el, o = {}) {
|
|
240
|
+
if (!canAnimate(el) || prefersReducedMotion())
|
|
241
|
+
return Promise.resolve();
|
|
242
|
+
const a = el.animate([...presetFrames(o.preset ?? 'fade', o.distance)].reverse(), timing(o, 250));
|
|
243
|
+
return a.finished.then(() => undefined, () => undefined);
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* How far an element has travelled through the screen: 0 when its top meets the bottom of the screen, 1 when its bottom
|
|
247
|
+
* leaves the top (the same range as CSS `view()` timelines, Scroll-driven Animations §3.2). Computed at most once a
|
|
248
|
+
* frame, only while the element is near the screen. Answers the function that stops it.
|
|
249
|
+
*/
|
|
250
|
+
export function scrollProgress(el, cb, o = {}) {
|
|
251
|
+
if (!el || typeof window === 'undefined' || typeof el.getBoundingClientRect !== 'function')
|
|
252
|
+
return () => { };
|
|
253
|
+
const name = o.cssVar && VAR_NAME.test(o.cssVar) ? o.cssVar : null;
|
|
254
|
+
const report = (p) => {
|
|
255
|
+
if (name)
|
|
256
|
+
setCssVars(el, { [name]: p });
|
|
257
|
+
cb?.(p);
|
|
258
|
+
};
|
|
259
|
+
if (o.staticWhenReduced && prefersReducedMotion()) {
|
|
260
|
+
report(1);
|
|
261
|
+
return () => { };
|
|
262
|
+
}
|
|
263
|
+
let frame = 0;
|
|
264
|
+
let pending = false;
|
|
265
|
+
let near = true;
|
|
266
|
+
const measure = () => {
|
|
267
|
+
pending = false;
|
|
268
|
+
const r = el.getBoundingClientRect();
|
|
269
|
+
const vh = window.innerHeight || 1;
|
|
270
|
+
const p = Math.min(1, Math.max(0, (vh - r.top) / (vh + r.height || 1)));
|
|
271
|
+
report(Math.round(p * 1e4) / 1e4);
|
|
272
|
+
};
|
|
273
|
+
const onScroll = () => {
|
|
274
|
+
if (!near || pending)
|
|
275
|
+
return;
|
|
276
|
+
pending = true;
|
|
277
|
+
frame = requestAnimationFrame(measure);
|
|
278
|
+
};
|
|
279
|
+
const io = typeof IntersectionObserver === 'undefined' ? null : new IntersectionObserver((es) => { near = es.some((e) => e.isIntersecting); if (near)
|
|
280
|
+
onScroll(); }, { rootMargin: '25% 0px' });
|
|
281
|
+
io?.observe(el);
|
|
282
|
+
window.addEventListener('scroll', onScroll, { passive: true });
|
|
283
|
+
window.addEventListener('resize', onScroll, { passive: true });
|
|
284
|
+
onScroll();
|
|
285
|
+
return () => {
|
|
286
|
+
io?.disconnect();
|
|
287
|
+
window.removeEventListener('scroll', onScroll);
|
|
288
|
+
window.removeEventListener('resize', onScroll);
|
|
289
|
+
if (pending)
|
|
290
|
+
cancelAnimationFrame(frame);
|
|
291
|
+
};
|
|
292
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/** PERCENTAGE: `value` is a percent; FIXED_AMOUNT: an amount in `currency`. */
|
|
2
|
+
export interface ReferralOffer {
|
|
3
|
+
type: 'PERCENTAGE' | 'FIXED_AMOUNT' | string;
|
|
4
|
+
value: string;
|
|
5
|
+
currency: string | null;
|
|
6
|
+
}
|
|
7
|
+
export interface ReferralReward {
|
|
8
|
+
type: 'PERCENTAGE_CODE' | 'FIXED_CODE' | 'STORE_CREDIT' | 'POINTS' | string;
|
|
9
|
+
value: string;
|
|
10
|
+
points: number;
|
|
11
|
+
currency: string | null;
|
|
12
|
+
}
|
|
13
|
+
/** The signed-in shopper's own link (they are the advocate). */
|
|
14
|
+
export interface ReferralState {
|
|
15
|
+
enabled: true;
|
|
16
|
+
code: string;
|
|
17
|
+
shareUrl: string;
|
|
18
|
+
offer: ReferralOffer;
|
|
19
|
+
reward: ReferralReward;
|
|
20
|
+
stats: {
|
|
21
|
+
clicks: number;
|
|
22
|
+
friendOrders: number;
|
|
23
|
+
pending: number;
|
|
24
|
+
rewarded: number;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/** What the `?ref=` link the shopper arrived on offers them (a new customer). */
|
|
28
|
+
export interface FriendOffer {
|
|
29
|
+
enabled: true;
|
|
30
|
+
code: string;
|
|
31
|
+
offer: ReferralOffer;
|
|
32
|
+
minimumOrder: string | null;
|
|
33
|
+
advocateFirstName: string | null;
|
|
34
|
+
/** The friend's own code once claimed by email (kept for the session). */
|
|
35
|
+
claimedCode: string | null;
|
|
36
|
+
}
|
|
37
|
+
export type ClaimResult = {
|
|
38
|
+
ok: true;
|
|
39
|
+
code: string;
|
|
40
|
+
} | {
|
|
41
|
+
ok: false;
|
|
42
|
+
error: 'email' | 'self' | 'failed';
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* `state` is null until known, for a guest and with referrals off; `friend` without a `?ref=` code this session,
|
|
46
|
+
* for a returning customer and with referrals off. `claim` (checkout then applies the friend's code) and `invite`
|
|
47
|
+
* (emails the link to a friend) never reject.
|
|
48
|
+
*/
|
|
49
|
+
export interface ReferralsValue {
|
|
50
|
+
state: ReferralState | null;
|
|
51
|
+
friend: FriendOffer | null;
|
|
52
|
+
claim(email: string): Promise<ClaimResult>;
|
|
53
|
+
invite(email: string): Promise<boolean>;
|
|
54
|
+
}
|
|
55
|
+
/** Provided by the theme runtime. Themes never render it. */
|
|
56
|
+
export declare const ReferralsContext: import("react").Context<ReferralsValue>;
|
|
57
|
+
export declare const useReferrals: () => ReferralsValue;
|
|
58
|
+
/** One tap per app: each target carries the link (and the message where the app takes text). */
|
|
59
|
+
export declare function shareTargets(url: string, text: string): {
|
|
60
|
+
id: 'line' | 'whatsapp' | 'messenger' | 'x';
|
|
61
|
+
href: string;
|
|
62
|
+
}[];
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The store's referral program as the shopper sees it (army #7751): the advocate's link, share targets and stats
|
|
3
|
+
* for the Referral tab, and the friend offer a `?ref=CODE` visit shows. The runtime provides it in the browser
|
|
4
|
+
* (lib/abi/referrals.tsx); on the server, in the editor and with referrals off both states are `null`.
|
|
5
|
+
*/
|
|
6
|
+
import { createContext, useContext } from 'react';
|
|
7
|
+
const OFF = { state: null, friend: null, claim: async () => ({ ok: false, error: 'failed' }), invite: async () => false };
|
|
8
|
+
/** Provided by the theme runtime. Themes never render it. */
|
|
9
|
+
export const ReferralsContext = createContext(OFF);
|
|
10
|
+
export const useReferrals = () => useContext(ReferralsContext);
|
|
11
|
+
/** One tap per app: each target carries the link (and the message where the app takes text). */
|
|
12
|
+
export function shareTargets(url, text) {
|
|
13
|
+
const u = encodeURIComponent(url);
|
|
14
|
+
return [
|
|
15
|
+
{ id: 'line', href: `https://social-plugins.line.me/lineit/share?url=${u}&text=${encodeURIComponent(text)}` },
|
|
16
|
+
{ id: 'whatsapp', href: `https://wa.me/?text=${encodeURIComponent(`${text} ${url}`)}` },
|
|
17
|
+
{ id: 'messenger', href: `fb-messenger://share/?link=${u}` },
|
|
18
|
+
{ id: 'x', href: `https://x.com/intent/post?text=${encodeURIComponent(text)}&url=${u}` },
|
|
19
|
+
];
|
|
20
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
export interface RewardsWay {
|
|
2
|
+
id: string;
|
|
3
|
+
kind: string;
|
|
4
|
+
name: string;
|
|
5
|
+
/** Fixed points for the action; null for an order, which earns at `program.earnRates`. */
|
|
6
|
+
points: number | null;
|
|
7
|
+
completed: boolean;
|
|
8
|
+
}
|
|
9
|
+
export interface RewardsReward {
|
|
10
|
+
id: string;
|
|
11
|
+
name: string;
|
|
12
|
+
kind: string;
|
|
13
|
+
pointsCost: number;
|
|
14
|
+
/** The member can turn points into this code now. */
|
|
15
|
+
affordable: boolean;
|
|
16
|
+
pointsNeeded: number;
|
|
17
|
+
tierLocked: boolean;
|
|
18
|
+
limitReached: boolean;
|
|
19
|
+
}
|
|
20
|
+
export interface RewardsCode {
|
|
21
|
+
code: string;
|
|
22
|
+
rewardName: string;
|
|
23
|
+
kind: string;
|
|
24
|
+
expiresAt: string | null;
|
|
25
|
+
}
|
|
26
|
+
export interface RewardsTier {
|
|
27
|
+
basis: 'POINTS' | 'SPEND' | string;
|
|
28
|
+
currency: string | null;
|
|
29
|
+
current: {
|
|
30
|
+
name: string;
|
|
31
|
+
earnMultiplier: number;
|
|
32
|
+
} | null;
|
|
33
|
+
next: {
|
|
34
|
+
name: string;
|
|
35
|
+
threshold: number;
|
|
36
|
+
} | null;
|
|
37
|
+
progress: {
|
|
38
|
+
qualifying: number;
|
|
39
|
+
remaining: number | null;
|
|
40
|
+
} | null;
|
|
41
|
+
}
|
|
42
|
+
export interface RewardsState {
|
|
43
|
+
enabled: true;
|
|
44
|
+
signedIn: boolean;
|
|
45
|
+
/** Signed in and in the program (a signed-in shopper may still have to join). */
|
|
46
|
+
member?: boolean;
|
|
47
|
+
/** `earnRates`: points per one unit of a currency, by ISO code. */
|
|
48
|
+
program: {
|
|
49
|
+
name: string;
|
|
50
|
+
earnRates: Readonly<Record<string, number>>;
|
|
51
|
+
};
|
|
52
|
+
balance?: {
|
|
53
|
+
available: number;
|
|
54
|
+
pending: number;
|
|
55
|
+
lifetimeEarned: number;
|
|
56
|
+
};
|
|
57
|
+
tier?: RewardsTier | null;
|
|
58
|
+
rewards?: readonly RewardsReward[];
|
|
59
|
+
codes?: readonly RewardsCode[];
|
|
60
|
+
ways: readonly RewardsWay[];
|
|
61
|
+
}
|
|
62
|
+
export type RedeemResult = {
|
|
63
|
+
ok: true;
|
|
64
|
+
code: string;
|
|
65
|
+
} | {
|
|
66
|
+
ok: false;
|
|
67
|
+
error: 'insufficient' | 'failed';
|
|
68
|
+
};
|
|
69
|
+
export interface RewardsValue {
|
|
70
|
+
/** null until known, and for a store with loyalty off. */
|
|
71
|
+
state: RewardsState | null;
|
|
72
|
+
/** Turns points into a code; the state is refreshed on success. Never rejects. */
|
|
73
|
+
redeem(rewardId: string): Promise<RedeemResult>;
|
|
74
|
+
/** Joins the program (a signed-in shopper who is not a member yet). Never rejects. */
|
|
75
|
+
join(): Promise<boolean>;
|
|
76
|
+
}
|
|
77
|
+
/** Provided by the theme runtime. Themes never render it. */
|
|
78
|
+
export declare const RewardsContext: import("react").Context<RewardsValue>;
|
|
79
|
+
/** The loyalty program, the shopper's balance and the redeem / join actions. */
|
|
80
|
+
export declare function useRewards(): RewardsValue;
|
|
81
|
+
/**
|
|
82
|
+
* Points an amount earns: amount x the currency's earn rate x the member's tier multiplier,
|
|
83
|
+
* rounded down. null when the program does not earn in that currency (the hint is then not shown).
|
|
84
|
+
*/
|
|
85
|
+
export declare function earnPoints(amount: number, currencyCode: string, state: RewardsState | null): number | null;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The store's loyalty program as the shopper sees it (army #7749): what the rewards launcher and
|
|
3
|
+
* the "Earn N points" hints render from, and the two things a shopper can ask of it.
|
|
4
|
+
*
|
|
5
|
+
* The theme runtime provides it after the page has loaded in the browser (lib/abi/rewards.tsx);
|
|
6
|
+
* on the server, in the editor and for a store with loyalty off it is `null`, so nothing renders.
|
|
7
|
+
*/
|
|
8
|
+
import { createContext, useContext } from 'react';
|
|
9
|
+
const OFF = { state: null, redeem: async () => ({ ok: false, error: 'failed' }), join: async () => false };
|
|
10
|
+
/** Provided by the theme runtime. Themes never render it. */
|
|
11
|
+
export const RewardsContext = createContext(OFF);
|
|
12
|
+
/** The loyalty program, the shopper's balance and the redeem / join actions. */
|
|
13
|
+
export function useRewards() {
|
|
14
|
+
return useContext(RewardsContext);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Points an amount earns: amount x the currency's earn rate x the member's tier multiplier,
|
|
18
|
+
* rounded down. null when the program does not earn in that currency (the hint is then not shown).
|
|
19
|
+
*/
|
|
20
|
+
export function earnPoints(amount, currencyCode, state) {
|
|
21
|
+
if (!state)
|
|
22
|
+
return null;
|
|
23
|
+
const rate = Number(state.program.earnRates[currencyCode]);
|
|
24
|
+
if (!(rate > 0) || !(amount > 0))
|
|
25
|
+
return null;
|
|
26
|
+
const multiplier = Number(state.tier?.current?.earnMultiplier) > 0 ? Number(state.tier.current.earnMultiplier) : 1;
|
|
27
|
+
// Rounded to 6 places first so 0.1 x 3 style float noise never drops a whole point.
|
|
28
|
+
return Math.floor(Math.round(amount * rate * multiplier * 1e6) / 1e6);
|
|
29
|
+
}
|