@tanqory/theme-kit 0.1.0 → 2.0.0-alpha.5
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 +98 -10
- package/dist/abi/canvas-live.d.ts +49 -0
- package/dist/abi/canvas-live.js +88 -0
- package/dist/abi/cart.d.ts +48 -0
- package/dist/abi/cart.js +321 -0
- package/dist/abi/convert.d.ts +192 -0
- package/dist/abi/convert.js +670 -0
- package/dist/abi/data-api.d.ts +15 -0
- package/dist/abi/data-api.js +198 -0
- package/dist/abi/jcs.d.ts +6 -0
- package/dist/abi/jcs.js +28 -0
- package/dist/abi/page-data.d.ts +209 -0
- package/dist/abi/page-data.js +583 -0
- package/dist/abi/page.d.ts +23 -0
- package/dist/abi/page.js +46 -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 +22 -0
- package/dist/abi/shell.js +10 -0
- package/dist/abi/wire.d.ts +38 -0
- package/dist/abi/wire.js +84 -0
- package/dist/build.d.ts +113 -0
- package/dist/build.js +294 -0
- package/dist/client.d.ts +19 -0
- package/dist/client.js +76 -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 +273 -0
- package/dist/headless/hooks.js +533 -0
- package/dist/headless/i18n.d.ts +13 -0
- package/dist/headless/i18n.js +45 -0
- package/dist/headless/index.d.ts +36 -0
- package/dist/headless/index.js +38 -0
- package/dist/headless/money.d.ts +25 -0
- package/dist/headless/money.js +61 -0
- package/dist/headless/page.d.ts +43 -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 +186 -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 +186 -0
- package/dist/headless/structured-data.d.ts +31 -0
- package/dist/headless/structured-data.js +57 -0
- package/dist/headless/theme.d.ts +99 -0
- package/dist/headless/theme.js +54 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +18 -553
- package/dist/runtime/data/StoreProvider.d.ts +56 -0
- package/dist/runtime/data/StoreProvider.js +308 -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 +154 -0
- package/dist/runtime/data/to-ui.d.ts +258 -0
- package/dist/runtime/data/to-ui.js +820 -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 +233 -0
- package/dist/runtime/kit/cart.d.ts +186 -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 +164 -0
- package/dist/runtime/kit/contract/content.js +83 -0
- package/dist/runtime/kit/contract/field-types.d.ts +290 -0
- package/dist/runtime/kit/contract/field-types.js +129 -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 +67 -0
- package/dist/runtime/kit/contract/validate.js +388 -0
- package/dist/runtime/kit/data.d.ts +636 -0
- package/dist/runtime/kit/data.js +1117 -0
- package/dist/{types → runtime/kit}/defineSection.d.ts +1 -1
- package/dist/runtime/kit/defineSection.js +10 -0
- package/dist/{types → runtime/kit}/dynamic-source.d.ts +4 -4
- package/dist/runtime/kit/dynamic-source.js +143 -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 +566 -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 +142 -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 +111 -0
- package/dist/runtime/render/listing-query.d.ts +47 -0
- package/dist/runtime/render/listing-query.js +114 -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 +240 -0
- package/dist/runtime/render/section-actions.d.ts +48 -0
- package/dist/runtime/render/section-actions.js +170 -0
- package/dist/runtime/render/starter.d.ts +11 -0
- package/dist/runtime/render/starter.js +81 -0
- package/dist/runtime/seo/head.d.ts +87 -0
- package/dist/runtime/seo/head.js +224 -0
- package/dist/runtime/seo/structured-data.d.ts +22 -0
- package/dist/runtime/seo/structured-data.js +115 -0
- package/dist/runtime/shell/PageShell.d.ts +18 -0
- package/dist/runtime/shell/PageShell.js +113 -0
- package/dist/runtime/shell/RuntimeBridge.d.ts +14 -0
- package/dist/runtime/shell/RuntimeBridge.js +85 -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/index.d.ts +24 -0
- package/dist/sdk/index.js +24 -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 +135 -0
- package/dist/sdk/runtime-context.js +73 -0
- package/dist/sdk/schema.d.ts +197 -0
- package/dist/sdk/schema.js +121 -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 +27 -0
- package/dist/sdk/store-lookup.js +29 -0
- package/dist/sdk/store.d.ts +115 -0
- package/dist/sdk/store.js +1 -0
- package/dist/sdk/sx.d.ts +40 -0
- package/dist/sdk/sx.js +112 -0
- package/dist/sdk/theme.d.ts +152 -0
- package/dist/sdk/theme.js +10 -0
- package/dist/sdk/types.d.ts +470 -0
- package/dist/sdk/types.js +7 -0
- package/dist/server.d.ts +65 -0
- package/dist/server.js +370 -0
- package/docs/headless-api.md +220 -0
- package/package.json +77 -23
- 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/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,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@tanqory/theme-kit/headless` — every storefront function, no styling. A theme supplies all markup and CSS;
|
|
3
|
+
* the kit supplies the data, the behaviour and accessible, semantic primitives carrying the platform's
|
|
4
|
+
* `tq-storefront-hooks/1` attributes (`data-tq-action`, `data-tq-price`, `data-tq-variant-picker`, …).
|
|
5
|
+
*
|
|
6
|
+
* declare defineTheme · defineSection · defineBlock · field · ai.*
|
|
7
|
+
* data useShop · useMenu · useProduct · useCollection · useSearch · usePredictiveSearch · useCart ·
|
|
8
|
+
* useCustomer · useLocalization · useRecommendations · useConsent · usePage
|
|
9
|
+
* commerce ProductForm · VariantPicker · ProductPrice · ProductQuantity · AddToCart · CartToggle · CartDrawer ·
|
|
10
|
+
* CartLine (+ Quantity / Increment / Decrement / Remove) · CheckoutButton · CollectionProvider ·
|
|
11
|
+
* FacetFilters · SortSelect · Pagination · SearchForm · LocalizationForm · NewsletterForm ·
|
|
12
|
+
* ContactForm · ConsentBanner · AccountSlot
|
|
13
|
+
* platform Seo · Image · SkipLink · useTranslate (t) · formatMoney · currencyDecimals · sx
|
|
14
|
+
*
|
|
15
|
+
* The server entry is `createRender({ manifest, bundle, storefront: {} })` (`@tanqory/theme-kit/server`); the
|
|
16
|
+
* browser entry is `hydrate({ bundle })` (`@tanqory/theme-kit/client`), which also applies live edits on the
|
|
17
|
+
* editor canvas. The manifest comes from `buildManifest` (`@tanqory/theme-kit/build`).
|
|
18
|
+
*/
|
|
19
|
+
// declare
|
|
20
|
+
export { defineTheme, defineSection, defineBlock } from './theme.js';
|
|
21
|
+
export { ai, aiProfileOf, checkAiProfile } from './ai.js';
|
|
22
|
+
export { field, defineSchema, resolveSettings } from '../sdk/schema.js';
|
|
23
|
+
export { defineSettings } from '../sdk/theme.js';
|
|
24
|
+
// data
|
|
25
|
+
export { useSettings, useShop, useMenu, useProduct, useProductState, useCollection, useCollectionState, useSearch, usePredictiveSearch, useCart, useCustomer, useLocalization, useRecommendations, useConsent, usePageContent, useBlog, useArticle, usePolicy, initialVariant, optionStates, ProductContext, CollectionContext, COLLECTION_SORTS, SEARCH_SORT_IDS, } from './hooks.js';
|
|
26
|
+
export { usePage, DETACHED_PAGE } from './page.js';
|
|
27
|
+
// commerce primitives
|
|
28
|
+
export { ProductForm, VariantPicker, ProductPrice, ProductQuantity, AddToCart, useProductForm } from './primitives/product.js';
|
|
29
|
+
export { CartToggle, CartDrawer, CartLine, CartLineQuantity, CartLineIncrement, CartLineDecrement, CartLineRemove, CheckoutButton, useCartLine } from './primitives/cart.js';
|
|
30
|
+
export { CollectionProvider, SortSelect, FacetFilters, Pagination } from './primitives/collection.js';
|
|
31
|
+
export { SearchForm, LocalizationForm, NewsletterForm, ContactForm, ConsentBanner, AccountSlot } from './primitives/storefront.js';
|
|
32
|
+
// platform
|
|
33
|
+
export { Seo, Image, SkipLink, resolveImageUrl } from './platform.js';
|
|
34
|
+
export { useTranslate, translate, interpolate, KIT_STRINGS, kitStrings } from './i18n.js';
|
|
35
|
+
export { formatMoney, currencyDecimals, currencySymbol } from './money.js';
|
|
36
|
+
export { productJsonLd, collectionJsonLd, breadcrumbJsonLd } from './structured-data.js';
|
|
37
|
+
export { sx, cx as sxJoin } from '../sdk/sx.js';
|
|
38
|
+
export { visuallyHidden } from './primitives/util.js';
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Money as a shopper reads it, in the currency's own minor units (JPY 0 decimals, THB 2, KWD 3).
|
|
3
|
+
*
|
|
4
|
+
* Deterministic on purpose: the server render and the hydrating browser must print the same characters, and
|
|
5
|
+
* `Intl.NumberFormat` currency output differs between engines (symbols, spaces, narrow no-break spaces). The
|
|
6
|
+
* digits are grouped by hand; only the number of decimals is read from `Intl` (ISO 4217 data every engine shares)
|
|
7
|
+
* for a currency the kit has no entry for.
|
|
8
|
+
*/
|
|
9
|
+
/** A price: `amount` as the data plane sends it (a decimal string) or as the theme types use it (a number). */
|
|
10
|
+
export interface MoneyLike {
|
|
11
|
+
amount: number | string;
|
|
12
|
+
currencyCode: string;
|
|
13
|
+
}
|
|
14
|
+
/** Minor units of an ISO 4217 currency (0 for JPY, 2 for USD/THB, 3 for KWD). */
|
|
15
|
+
export declare function currencyDecimals(currencyCode: string): number;
|
|
16
|
+
/** The symbol the kit prints for a currency, or '' when it prints the code instead. */
|
|
17
|
+
export declare function currencySymbol(currencyCode: string): string;
|
|
18
|
+
export interface FormatMoneyOptions {
|
|
19
|
+
/** `symbol` "$37.50" (default) · `code` "37.50 USD" · `symbol-code` "$37.50 USD" · `amount` "37.50". */
|
|
20
|
+
display?: 'symbol' | 'code' | 'symbol-code' | 'amount';
|
|
21
|
+
/** Drop a zero fraction ("$40" for 40.00). Default false. */
|
|
22
|
+
trimZeros?: boolean;
|
|
23
|
+
}
|
|
24
|
+
/** "$37.50", "¥5,600", "฿1,234.00". An amount that is not a number prints as given, with its code. */
|
|
25
|
+
export declare function formatMoney(money: MoneyLike | null | undefined, options?: FormatMoneyOptions): string;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Money as a shopper reads it, in the currency's own minor units (JPY 0 decimals, THB 2, KWD 3).
|
|
3
|
+
*
|
|
4
|
+
* Deterministic on purpose: the server render and the hydrating browser must print the same characters, and
|
|
5
|
+
* `Intl.NumberFormat` currency output differs between engines (symbols, spaces, narrow no-break spaces). The
|
|
6
|
+
* digits are grouped by hand; only the number of decimals is read from `Intl` (ISO 4217 data every engine shares)
|
|
7
|
+
* for a currency the kit has no entry for.
|
|
8
|
+
*/
|
|
9
|
+
const SYMBOLS = {
|
|
10
|
+
USD: '$', THB: '฿', EUR: '€', GBP: '£', JPY: '¥', KRW: '₩', VND: '₫', IDR: 'Rp', SGD: 'S$', HKD: 'HK$', TWD: 'NT$',
|
|
11
|
+
AUD: 'A$', CAD: 'CA$', NZD: 'NZ$', CNY: 'CN¥', INR: '₹', MYR: 'RM', PHP: '₱', CHF: 'CHF ', SEK: 'kr ', NOK: 'kr ',
|
|
12
|
+
DKK: 'kr ', MXN: 'MX$', BRL: 'R$', AED: 'AED ', SAR: 'SAR ', ILS: '₪', TRY: '₺', PLN: 'zł ', ZAR: 'R ',
|
|
13
|
+
};
|
|
14
|
+
const DECIMALS = {
|
|
15
|
+
JPY: 0, KRW: 0, VND: 0, IDR: 0, CLP: 0, ISK: 0, HUF: 0, UGX: 0, PYG: 0, XAF: 0, XOF: 0, KWD: 3, BHD: 3, OMR: 3, JOD: 3, TND: 3,
|
|
16
|
+
};
|
|
17
|
+
/** Minor units of an ISO 4217 currency (0 for JPY, 2 for USD/THB, 3 for KWD). */
|
|
18
|
+
export function currencyDecimals(currencyCode) {
|
|
19
|
+
const code = String(currencyCode || '').toUpperCase();
|
|
20
|
+
const known = DECIMALS[code];
|
|
21
|
+
if (known !== undefined)
|
|
22
|
+
return known;
|
|
23
|
+
if (!/^[A-Z]{3}$/.test(code))
|
|
24
|
+
return 2;
|
|
25
|
+
try {
|
|
26
|
+
const d = new Intl.NumberFormat('en', { style: 'currency', currency: code }).resolvedOptions().maximumFractionDigits;
|
|
27
|
+
return typeof d === 'number' && d >= 0 && d <= 4 ? d : 2;
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return 2;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/** The symbol the kit prints for a currency, or '' when it prints the code instead. */
|
|
34
|
+
export function currencySymbol(currencyCode) {
|
|
35
|
+
return SYMBOLS[String(currencyCode || '').toUpperCase()]?.trim() ?? '';
|
|
36
|
+
}
|
|
37
|
+
/** "$37.50", "¥5,600", "฿1,234.00". An amount that is not a number prints as given, with its code. */
|
|
38
|
+
export function formatMoney(money, options = {}) {
|
|
39
|
+
if (!money)
|
|
40
|
+
return '';
|
|
41
|
+
const code = String(money.currencyCode || '').toUpperCase();
|
|
42
|
+
const n = typeof money.amount === 'number' ? money.amount : Number(money.amount);
|
|
43
|
+
if (!Number.isFinite(n))
|
|
44
|
+
return `${money.amount} ${code}`.trim();
|
|
45
|
+
const decimals = currencyDecimals(code);
|
|
46
|
+
let [int, frac = ''] = Math.abs(n).toFixed(decimals).split('.');
|
|
47
|
+
if (options.trimZeros && /^0*$/.test(frac))
|
|
48
|
+
frac = '';
|
|
49
|
+
const digits = `${n < 0 ? '-' : ''}${int.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}${frac ? `.${frac}` : ''}`;
|
|
50
|
+
const symbol = SYMBOLS[code];
|
|
51
|
+
switch (options.display ?? 'symbol') {
|
|
52
|
+
case 'amount':
|
|
53
|
+
return digits;
|
|
54
|
+
case 'code':
|
|
55
|
+
return `${digits} ${code}`;
|
|
56
|
+
case 'symbol-code':
|
|
57
|
+
return symbol ? `${symbol}${digits} ${code}` : `${digits} ${code}`;
|
|
58
|
+
default:
|
|
59
|
+
return symbol ? `${symbol}${digits}` : `${digits} ${code}`;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** What `usePage()` returns. Every member is public page data (it is printed into the page's state block). */
|
|
2
|
+
export interface PageInfo {
|
|
3
|
+
/** `ctx.route.kind`: `home`, `product`, `collection`, `search`, `cart`, `page`, `blog`, `article`, `policy`, `customer`, `not_found`, `custom:<id>`. */
|
|
4
|
+
kind: string;
|
|
5
|
+
/** The route resource's handle (product, collection, page, blog, article, policy). */
|
|
6
|
+
handle?: string;
|
|
7
|
+
/** The article's blog handle on the `article` kind. */
|
|
8
|
+
blogHandle?: string;
|
|
9
|
+
/** Query params the platform decoded for this route (`page`, `sort`, `q`, `variant`, `tag`, …), strings only. */
|
|
10
|
+
params: Readonly<Record<string, string>>;
|
|
11
|
+
/** `https://<host><path>`: no query, no fragment (`ctx.route.canonical`). */
|
|
12
|
+
canonical: string;
|
|
13
|
+
/** The page's content language (`ctx.locale.language`). */
|
|
14
|
+
language: string;
|
|
15
|
+
/** The language the theme's own strings are drawn from (a bundled `locales/<ui>.json`). */
|
|
16
|
+
ui: string;
|
|
17
|
+
/** Every language the store publishes. */
|
|
18
|
+
languages: readonly string[];
|
|
19
|
+
/** A preview render (the editor canvas or a preview link), not the live storefront. */
|
|
20
|
+
preview: boolean;
|
|
21
|
+
/** Base URL of this artifact's client files (`ctx.assets.base`): `asset:client/<file>` resolves under it. */
|
|
22
|
+
assetsBase: string;
|
|
23
|
+
/** The merchant's dashboard wording for the theme's strings, where it differs from the theme's own (`ctx.data.localization.strings`). */
|
|
24
|
+
strings: Readonly<Record<string, string>>;
|
|
25
|
+
}
|
|
26
|
+
/** A page with no platform around it (a unit test, a component preview): English, the home route, no overrides. */
|
|
27
|
+
export declare const DETACHED_PAGE: PageInfo;
|
|
28
|
+
/** Provided by the server render. In the browser it is absent and the hydration state answers instead. */
|
|
29
|
+
export declare const PageInfoContext: import("react").Context<PageInfo | null>;
|
|
30
|
+
/**
|
|
31
|
+
* Head contributions collected during ONE server render (`<Seo>`): the server reads them after
|
|
32
|
+
* `renderToString` and prints them in `<head>`. Absent in the browser, where `<Seo>` renders nothing.
|
|
33
|
+
*/
|
|
34
|
+
export interface HeadSink {
|
|
35
|
+
title?: string;
|
|
36
|
+
description?: string;
|
|
37
|
+
image?: string;
|
|
38
|
+
noindex?: boolean;
|
|
39
|
+
jsonLd: Record<string, unknown>[];
|
|
40
|
+
}
|
|
41
|
+
export declare const HeadSinkContext: import("react").Context<HeadSink | null>;
|
|
42
|
+
/** The page this component renders on (route, params, languages, assets, merchant wording). */
|
|
43
|
+
export declare function usePage(): PageInfo;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The page facts a headless theme reads that the section data does not carry: which route this is (kind, handle,
|
|
3
|
+
* the query params the platform decoded), the canonical URL, the languages the store publishes, where the
|
|
4
|
+
* artifact's assets live, and the merchant's own wording for the theme's strings.
|
|
5
|
+
*
|
|
6
|
+
* server `createRender({ storefront })` provides the value and copies it into the hydration state (`kit`);
|
|
7
|
+
* browser the same value is read back from that state, so the hydrating render sees the same bytes.
|
|
8
|
+
*
|
|
9
|
+
* Nothing here is cached across renders on the server: the value is created inside `render()`. In the browser a
|
|
10
|
+
* page is one store and one render tree, so the parsed state is kept for the page's lifetime.
|
|
11
|
+
*/
|
|
12
|
+
import { createContext, useContext } from 'react';
|
|
13
|
+
/** A page with no platform around it (a unit test, a component preview): English, the home route, no overrides. */
|
|
14
|
+
export const DETACHED_PAGE = {
|
|
15
|
+
kind: 'home',
|
|
16
|
+
params: {},
|
|
17
|
+
canonical: '',
|
|
18
|
+
language: 'en',
|
|
19
|
+
ui: 'en',
|
|
20
|
+
languages: ['en'],
|
|
21
|
+
preview: false,
|
|
22
|
+
assetsBase: '',
|
|
23
|
+
strings: {},
|
|
24
|
+
};
|
|
25
|
+
/** Provided by the server render. In the browser it is absent and the hydration state answers instead. */
|
|
26
|
+
export const PageInfoContext = createContext(null);
|
|
27
|
+
export const HeadSinkContext = createContext(null);
|
|
28
|
+
let clientPage = null;
|
|
29
|
+
/** The `kit` member of the page's `<script type="application/json" id="tq-state">`, parsed once per page. */
|
|
30
|
+
function pageFromState() {
|
|
31
|
+
if (typeof document === 'undefined')
|
|
32
|
+
return DETACHED_PAGE;
|
|
33
|
+
const el = document.getElementById('tq-state');
|
|
34
|
+
if (!el)
|
|
35
|
+
return DETACHED_PAGE;
|
|
36
|
+
if (clientPage && clientPage.el === el)
|
|
37
|
+
return clientPage.value;
|
|
38
|
+
let value = DETACHED_PAGE;
|
|
39
|
+
try {
|
|
40
|
+
const kit = JSON.parse(el.textContent ?? '{}').kit;
|
|
41
|
+
if (kit && typeof kit === 'object' && typeof kit.kind === 'string')
|
|
42
|
+
value = { ...DETACHED_PAGE, ...kit };
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
value = DETACHED_PAGE;
|
|
46
|
+
}
|
|
47
|
+
clientPage = { el, value };
|
|
48
|
+
return value;
|
|
49
|
+
}
|
|
50
|
+
/** The page this component renders on (route, params, languages, assets, merchant wording). */
|
|
51
|
+
export function usePage() {
|
|
52
|
+
return useContext(PageInfoContext) ?? pageFromState();
|
|
53
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Platform helpers of the headless API: `<Image>` with the platform's asset rules, `<Seo>` for the document head,
|
|
3
|
+
* and `<SkipLink>` to the page's main content.
|
|
4
|
+
*/
|
|
5
|
+
import { type ImgHTMLAttributes, type ReactNode } from 'react';
|
|
6
|
+
import type { Image as ImageData } from '../sdk/types.js';
|
|
7
|
+
import type { JsonLd } from './structured-data.js';
|
|
8
|
+
/**
|
|
9
|
+
* An image URL a page may print: an absolute `https:` URL as given, or `asset:client/<file>` (a file the theme
|
|
10
|
+
* ships, valid in its defaults and presets) under the artifact's asset base. Anything else is refused (null):
|
|
11
|
+
* `http:`, `data:`, `javascript:`, a relative path.
|
|
12
|
+
*/
|
|
13
|
+
export declare function resolveImageUrl(value: string | null | undefined, assetsBase: string): string | null;
|
|
14
|
+
type ImageInput = ImageData | {
|
|
15
|
+
url?: string | null;
|
|
16
|
+
altText?: string | null;
|
|
17
|
+
width?: number | null;
|
|
18
|
+
height?: number | null;
|
|
19
|
+
} | string | null | undefined;
|
|
20
|
+
export interface ImageProps extends Omit<ImgHTMLAttributes<HTMLImageElement>, 'src' | 'alt'> {
|
|
21
|
+
/** An image from the data (`Product.featuredImage`, a variant's image), or an image setting's value. */
|
|
22
|
+
image?: ImageInput;
|
|
23
|
+
/** Alternative text; the image's own `alt` by default, '' (decorative) when it has none. */
|
|
24
|
+
alt?: string;
|
|
25
|
+
/** What renders when there is no usable image (nothing by default). */
|
|
26
|
+
fallback?: ReactNode;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* `<img>` under the platform's asset rules (`resolveImageUrl`), with its intrinsic width and height (no layout
|
|
30
|
+
* shift), lazy loading and async decoding by default.
|
|
31
|
+
*/
|
|
32
|
+
export declare function Image({ image, alt, fallback, loading, decoding, width, height, ...rest }: ImageProps): JSX.Element | null;
|
|
33
|
+
export interface SeoProps {
|
|
34
|
+
/** The document title, verbatim (the default is the resource's SEO title, else "<title> — <shop>"). */
|
|
35
|
+
title?: string;
|
|
36
|
+
description?: string;
|
|
37
|
+
/** An absolute https: URL for the share image. */
|
|
38
|
+
image?: string;
|
|
39
|
+
/** Ask search engines not to index this page. */
|
|
40
|
+
noindex?: boolean;
|
|
41
|
+
/** Extra schema.org objects. A Product object is ignored: the render already prints exactly one on a product page. */
|
|
42
|
+
jsonLd?: JsonLd | readonly JsonLd[];
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* What this page says about itself in `<head>`. Rendered in a section or the Layout during the server render
|
|
46
|
+
* (`createRender({ storefront })`); renders nothing itself, and nothing at all in the browser.
|
|
47
|
+
* The defaults need no `<Seo>`: title, description, canonical, Open Graph, hreflang, Product, Collection and
|
|
48
|
+
* Breadcrumb JSON-LD are printed by the render from the page's data.
|
|
49
|
+
*/
|
|
50
|
+
export declare function Seo({ title, description, image, noindex, jsonLd }: SeoProps): null;
|
|
51
|
+
/** "Skip to content": the first focusable element of the page, to `#main` (the `<main>` the section renderer wraps the page body in). */
|
|
52
|
+
export declare function SkipLink({ className }: {
|
|
53
|
+
className?: string;
|
|
54
|
+
}): JSX.Element;
|
|
55
|
+
export {};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* Platform helpers of the headless API: `<Image>` with the platform's asset rules, `<Seo>` for the document head,
|
|
4
|
+
* and `<SkipLink>` to the page's main content.
|
|
5
|
+
*/
|
|
6
|
+
import { useContext } from 'react';
|
|
7
|
+
import { HeadSinkContext, usePage } from './page.js';
|
|
8
|
+
import { useTranslate } from './i18n.js';
|
|
9
|
+
const ASSET = /^asset:client\/([A-Za-z0-9._/-]+)$/;
|
|
10
|
+
/**
|
|
11
|
+
* An image URL a page may print: an absolute `https:` URL as given, or `asset:client/<file>` (a file the theme
|
|
12
|
+
* ships, valid in its defaults and presets) under the artifact's asset base. Anything else is refused (null):
|
|
13
|
+
* `http:`, `data:`, `javascript:`, a relative path.
|
|
14
|
+
*/
|
|
15
|
+
export function resolveImageUrl(value, assetsBase) {
|
|
16
|
+
const v = typeof value === 'string' ? value.trim() : '';
|
|
17
|
+
if (!v)
|
|
18
|
+
return null;
|
|
19
|
+
const asset = ASSET.exec(v);
|
|
20
|
+
if (asset) {
|
|
21
|
+
if (asset[1].split('/').some((seg) => seg === '..' || seg === '.'))
|
|
22
|
+
return null;
|
|
23
|
+
return /^https:\/\//.test(assetsBase) ? `${assetsBase.replace(/\/+$/, '')}/${asset[1]}` : null;
|
|
24
|
+
}
|
|
25
|
+
try {
|
|
26
|
+
const u = new URL(v);
|
|
27
|
+
return u.protocol === 'https:' && u.hostname ? u.toString() : null;
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* `<img>` under the platform's asset rules (`resolveImageUrl`), with its intrinsic width and height (no layout
|
|
35
|
+
* shift), lazy loading and async decoding by default.
|
|
36
|
+
*/
|
|
37
|
+
export function Image({ image, alt, fallback = null, loading = 'lazy', decoding = 'async', width, height, ...rest }) {
|
|
38
|
+
const page = usePage();
|
|
39
|
+
const obj = image && typeof image === 'object' ? image : null;
|
|
40
|
+
const raw = typeof image === 'string' ? image : (obj?.src ?? obj?.url);
|
|
41
|
+
const src = resolveImageUrl(raw, page.assetsBase);
|
|
42
|
+
if (!src)
|
|
43
|
+
return _jsx(_Fragment, { children: fallback });
|
|
44
|
+
const w = width ?? (typeof obj?.width === 'number' && obj.width > 0 ? obj.width : undefined);
|
|
45
|
+
const h = height ?? (typeof obj?.height === 'number' && obj.height > 0 ? obj.height : undefined);
|
|
46
|
+
const srcSet = typeof obj?.srcSet === 'string' ? obj.srcSet : undefined;
|
|
47
|
+
const text = alt ?? (obj?.alt ?? obj?.altText) ?? '';
|
|
48
|
+
return _jsx("img", { ...rest, src: src, ...(srcSet ? { srcSet } : {}), alt: text, ...(w ? { width: w } : {}), ...(h ? { height: h } : {}), loading: loading, decoding: decoding });
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* What this page says about itself in `<head>`. Rendered in a section or the Layout during the server render
|
|
52
|
+
* (`createRender({ storefront })`); renders nothing itself, and nothing at all in the browser.
|
|
53
|
+
* The defaults need no `<Seo>`: title, description, canonical, Open Graph, hreflang, Product, Collection and
|
|
54
|
+
* Breadcrumb JSON-LD are printed by the render from the page's data.
|
|
55
|
+
*/
|
|
56
|
+
export function Seo({ title, description, image, noindex, jsonLd }) {
|
|
57
|
+
const sink = useContext(HeadSinkContext);
|
|
58
|
+
if (!sink)
|
|
59
|
+
return null;
|
|
60
|
+
if (title?.trim())
|
|
61
|
+
sink.title = title.trim();
|
|
62
|
+
if (description?.trim())
|
|
63
|
+
sink.description = description.trim();
|
|
64
|
+
if (image && /^https:\/\//.test(image))
|
|
65
|
+
sink.image = image;
|
|
66
|
+
if (noindex)
|
|
67
|
+
sink.noindex = true;
|
|
68
|
+
for (const b of jsonLd ? (Array.isArray(jsonLd) ? jsonLd : [jsonLd]) : [])
|
|
69
|
+
if (b && typeof b === 'object')
|
|
70
|
+
sink.jsonLd.push(b);
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
/** "Skip to content": the first focusable element of the page, to `#main` (the `<main>` the section renderer wraps the page body in). */
|
|
74
|
+
export function SkipLink({ className }) {
|
|
75
|
+
const t = useTranslate();
|
|
76
|
+
return (_jsx("a", { className: className, href: "#main", children: t('a11y.skipToContent') }));
|
|
77
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cart primitives. The cart itself is the runtime's (versioned `/_tq/actions/cart.*`, one request per line at a
|
|
3
|
+
* time, conflicts retried); these render its controls.
|
|
4
|
+
*
|
|
5
|
+
* <CartToggle /> <a href="/cart" data-tq-action="cart:open">: opens the drawer (or goes to /cart)
|
|
6
|
+
* <CartDrawer>…</…> role="dialog" aria-modal while open: focus moves in, Tab stays in, Escape closes,
|
|
7
|
+
* focus returns to the opener
|
|
8
|
+
* <CartLine line> one line's state for the controls inside it:
|
|
9
|
+
* <CartLineQuantity /> <input type="number" data-tq-quantity>, saved on Enter / blur
|
|
10
|
+
* <CartLineIncrement /> <CartLineDecrement /> data-tq-action="cart:increment" / step down
|
|
11
|
+
* <CartLineRemove /> data-tq-action="cart:remove"
|
|
12
|
+
* <CheckoutButton /> <a data-tq-action="cart:checkout" href={cart.checkoutUrl}>: checkout is the platform's
|
|
13
|
+
*/
|
|
14
|
+
import { type AnchorHTMLAttributes, type ButtonHTMLAttributes, type ReactNode } from 'react';
|
|
15
|
+
import type { CartLine as CartLineData } from '../../sdk/types.js';
|
|
16
|
+
import type { CartLineRefusal } from '../../sdk/runtime-context.js';
|
|
17
|
+
import { type CartStateHeadless } from '../hooks.js';
|
|
18
|
+
import { type RenderChildren } from './util.js';
|
|
19
|
+
export interface CartToggleProps extends Omit<AnchorHTMLAttributes<HTMLAnchorElement>, 'href' | 'children'> {
|
|
20
|
+
/** Open the cart drawer (default). `false`: the link goes to the cart page. */
|
|
21
|
+
drawer?: boolean;
|
|
22
|
+
children?: RenderChildren<{
|
|
23
|
+
itemCount: number;
|
|
24
|
+
isOpen: boolean;
|
|
25
|
+
label: string;
|
|
26
|
+
}>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The header's cart control: a link to `/cart` that opens the drawer instead when there is one. The item count
|
|
30
|
+
* renders in `<span data-tq-client-slot="cart-count">` (the server never knows the shopper's cart); a theme that
|
|
31
|
+
* renders its own count from `children` should do the same.
|
|
32
|
+
*/
|
|
33
|
+
export declare function CartToggle({ drawer, children, onClick, ...rest }: CartToggleProps): JSX.Element;
|
|
34
|
+
export interface CartDrawerProps {
|
|
35
|
+
className?: string;
|
|
36
|
+
/** A full-screen layer behind the dialog; clicking it closes the drawer. */
|
|
37
|
+
backdropClassName?: string;
|
|
38
|
+
/** The dialog's accessible name; "Your cart" by default. */
|
|
39
|
+
label?: string;
|
|
40
|
+
children?: RenderChildren<CartStateHeadless>;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The cart drawer's behaviour around the theme's own markup: rendered only while the cart is open (after an add,
|
|
44
|
+
* or from `<CartToggle>`), as a modal dialog that takes focus, keeps Tab inside, closes on Escape and gives focus
|
|
45
|
+
* back to what opened it.
|
|
46
|
+
*/
|
|
47
|
+
export declare function CartDrawer({ className, backdropClassName, label, children }: CartDrawerProps): JSX.Element;
|
|
48
|
+
export interface CartLineState {
|
|
49
|
+
line: CartLineData;
|
|
50
|
+
busy: boolean;
|
|
51
|
+
failed: boolean;
|
|
52
|
+
refusal: CartLineRefusal | null;
|
|
53
|
+
setQuantity(quantity: number): Promise<void>;
|
|
54
|
+
increment(): Promise<void>;
|
|
55
|
+
decrement(): Promise<void>;
|
|
56
|
+
remove(): Promise<void>;
|
|
57
|
+
}
|
|
58
|
+
/** The enclosing `<CartLine>`'s state, or null outside one. */
|
|
59
|
+
export declare function useCartLine(): CartLineState | null;
|
|
60
|
+
export interface CartLineProps {
|
|
61
|
+
line: CartLineData;
|
|
62
|
+
/** `li` by default (lines are a list); `div` or `tr` for other layouts. */
|
|
63
|
+
as?: 'li' | 'div' | 'tr' | 'article';
|
|
64
|
+
className?: string;
|
|
65
|
+
children?: RenderChildren<CartLineState>;
|
|
66
|
+
}
|
|
67
|
+
/** One cart line: gives the line's controls their line (and marks busy / failed lines with data attributes). */
|
|
68
|
+
export declare function CartLine({ line, as, className, children }: CartLineProps): JSX.Element;
|
|
69
|
+
export interface CartLineQuantityProps {
|
|
70
|
+
className?: string;
|
|
71
|
+
labelClassName?: string;
|
|
72
|
+
label?: 'visible' | 'hidden';
|
|
73
|
+
}
|
|
74
|
+
/** The line's quantity (`data-tq-quantity`): typed freely, saved on Enter or when it loses focus; 0 removes the line. */
|
|
75
|
+
export declare function CartLineQuantity({ className, labelClassName, label }: CartLineQuantityProps): JSX.Element;
|
|
76
|
+
type LineButtonProps = Omit<ButtonHTMLAttributes<HTMLButtonElement>, 'type' | 'onClick'> & {
|
|
77
|
+
children?: ReactNode;
|
|
78
|
+
};
|
|
79
|
+
/** Quantity + 1 (`data-tq-action="cart:increment"`). Its accessible name is the "Increase quantity" string. */
|
|
80
|
+
export declare function CartLineIncrement({ children, disabled, ...rest }: LineButtonProps): JSX.Element;
|
|
81
|
+
/** Quantity − 1; at 1 it removes the line. Its accessible name is the "Decrease quantity" string. */
|
|
82
|
+
export declare function CartLineDecrement({ children, disabled, ...rest }: LineButtonProps): JSX.Element;
|
|
83
|
+
/** Remove the line (`data-tq-action="cart:remove"`). */
|
|
84
|
+
export declare function CartLineRemove({ children, disabled, ...rest }: LineButtonProps): JSX.Element;
|
|
85
|
+
export interface CheckoutButtonProps extends Omit<AnchorHTMLAttributes<HTMLAnchorElement>, 'href'> {
|
|
86
|
+
children?: ReactNode;
|
|
87
|
+
}
|
|
88
|
+
/** The checkout link: exactly `Cart.checkoutUrl` (`data-tq-action="cart:checkout"`). Checkout never renders in a theme. */
|
|
89
|
+
export declare function CheckoutButton({ children, ...rest }: CheckoutButtonProps): JSX.Element;
|
|
90
|
+
export {};
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
/**
|
|
3
|
+
* Cart primitives. The cart itself is the runtime's (versioned `/_tq/actions/cart.*`, one request per line at a
|
|
4
|
+
* time, conflicts retried); these render its controls.
|
|
5
|
+
*
|
|
6
|
+
* <CartToggle /> <a href="/cart" data-tq-action="cart:open">: opens the drawer (or goes to /cart)
|
|
7
|
+
* <CartDrawer>…</…> role="dialog" aria-modal while open: focus moves in, Tab stays in, Escape closes,
|
|
8
|
+
* focus returns to the opener
|
|
9
|
+
* <CartLine line> one line's state for the controls inside it:
|
|
10
|
+
* <CartLineQuantity /> <input type="number" data-tq-quantity>, saved on Enter / blur
|
|
11
|
+
* <CartLineIncrement /> <CartLineDecrement /> data-tq-action="cart:increment" / step down
|
|
12
|
+
* <CartLineRemove /> data-tq-action="cart:remove"
|
|
13
|
+
* <CheckoutButton /> <a data-tq-action="cart:checkout" href={cart.checkoutUrl}>: checkout is the platform's
|
|
14
|
+
*/
|
|
15
|
+
import { createContext, useContext, useEffect, useId, useRef, useState } from 'react';
|
|
16
|
+
import { useCart } from '../hooks.js';
|
|
17
|
+
import { useTranslate } from '../i18n.js';
|
|
18
|
+
import { cn, focusables, renderChildren, trapTab, visuallyHidden } from './util.js';
|
|
19
|
+
/**
|
|
20
|
+
* The header's cart control: a link to `/cart` that opens the drawer instead when there is one. The item count
|
|
21
|
+
* renders in `<span data-tq-client-slot="cart-count">` (the server never knows the shopper's cart); a theme that
|
|
22
|
+
* renders its own count from `children` should do the same.
|
|
23
|
+
*/
|
|
24
|
+
export function CartToggle({ drawer = true, children, onClick, ...rest }) {
|
|
25
|
+
const cart = useCart();
|
|
26
|
+
const t = useTranslate();
|
|
27
|
+
const label = t('cart.openCart');
|
|
28
|
+
return (_jsx("a", { ...rest, href: "/cart", "data-tq-action": "cart:open", ...(drawer ? { 'aria-haspopup': 'dialog', 'aria-expanded': cart.isOpen } : {}), onClick: (e) => {
|
|
29
|
+
onClick?.(e);
|
|
30
|
+
if (e.defaultPrevented || !drawer || e.metaKey || e.ctrlKey || e.shiftKey || e.button !== 0)
|
|
31
|
+
return;
|
|
32
|
+
e.preventDefault();
|
|
33
|
+
cart.open();
|
|
34
|
+
}, children: renderChildren(children, { itemCount: cart.itemCount, isOpen: cart.isOpen, label }, () => (_jsxs(_Fragment, { children: [label, _jsx("span", { "data-tq-client-slot": "cart-count", children: cart.itemCount > 0 ? ` (${cart.itemCount})` : null })] }))) }));
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The cart drawer's behaviour around the theme's own markup: rendered only while the cart is open (after an add,
|
|
38
|
+
* or from `<CartToggle>`), as a modal dialog that takes focus, keeps Tab inside, closes on Escape and gives focus
|
|
39
|
+
* back to what opened it.
|
|
40
|
+
*/
|
|
41
|
+
export function CartDrawer({ className, backdropClassName, label, children }) {
|
|
42
|
+
const cart = useCart();
|
|
43
|
+
const t = useTranslate();
|
|
44
|
+
const ref = useRef(null);
|
|
45
|
+
const { isOpen, close } = cart;
|
|
46
|
+
useEffect(() => {
|
|
47
|
+
if (!isOpen || typeof document === 'undefined')
|
|
48
|
+
return;
|
|
49
|
+
const el = ref.current;
|
|
50
|
+
const opener = document.activeElement;
|
|
51
|
+
(el ? focusables(el)[0] ?? el : null)?.focus();
|
|
52
|
+
const onKey = (e) => {
|
|
53
|
+
if (e.key === 'Escape') {
|
|
54
|
+
e.preventDefault();
|
|
55
|
+
close();
|
|
56
|
+
}
|
|
57
|
+
else if (el)
|
|
58
|
+
trapTab(e, el);
|
|
59
|
+
};
|
|
60
|
+
document.addEventListener('keydown', onKey);
|
|
61
|
+
return () => {
|
|
62
|
+
document.removeEventListener('keydown', onKey);
|
|
63
|
+
if (opener && typeof opener.focus === 'function' && opener.isConnected)
|
|
64
|
+
opener.focus();
|
|
65
|
+
};
|
|
66
|
+
}, [isOpen, close]);
|
|
67
|
+
// Browser-only: an empty client slot in the server HTML (theme-abi §10.5, Hyd-2).
|
|
68
|
+
return (_jsx("div", { "data-tq-client-slot": "cart-drawer", children: isOpen ? (_jsxs(_Fragment, { children: [backdropClassName ? _jsx("div", { className: backdropClassName, "aria-hidden": "true", onClick: close }) : null, _jsx("div", { ref: ref, className: className, role: "dialog", "aria-modal": "true", "aria-label": label ?? t('cart.title'), tabIndex: -1, "data-tq-cart-drawer": "", children: renderChildren(children, cart, () => null) })] })) : null }));
|
|
69
|
+
}
|
|
70
|
+
const LineContext = createContext(null);
|
|
71
|
+
/** The enclosing `<CartLine>`'s state, or null outside one. */
|
|
72
|
+
export function useCartLine() {
|
|
73
|
+
return useContext(LineContext);
|
|
74
|
+
}
|
|
75
|
+
/** One cart line: gives the line's controls their line (and marks busy / failed lines with data attributes). */
|
|
76
|
+
export function CartLine({ line, as = 'li', className, children }) {
|
|
77
|
+
const cart = useCart();
|
|
78
|
+
const state = {
|
|
79
|
+
line,
|
|
80
|
+
busy: cart.isBusy(line.id),
|
|
81
|
+
failed: cart.hasFailed(line.id),
|
|
82
|
+
refusal: cart.refusal(line.id),
|
|
83
|
+
setQuantity: (q) => cart.setQuantity(line.id, q),
|
|
84
|
+
increment: () => cart.setQuantity(line.id, line.quantity + 1),
|
|
85
|
+
decrement: () => (line.quantity <= 1 ? cart.remove(line.id) : cart.setQuantity(line.id, line.quantity - 1)),
|
|
86
|
+
remove: () => cart.remove(line.id),
|
|
87
|
+
};
|
|
88
|
+
const Tag = as;
|
|
89
|
+
return (_jsx(LineContext.Provider, { value: state, children: _jsx(Tag, { className: className, "data-tq-cart-line": line.id, "data-busy": state.busy ? 'true' : undefined, "data-failed": state.failed ? 'true' : undefined, children: renderChildren(children, state, () => null) }) }));
|
|
90
|
+
}
|
|
91
|
+
function useLineOrThrow(name) {
|
|
92
|
+
const s = useCartLine();
|
|
93
|
+
if (!s)
|
|
94
|
+
throw new Error(`[theme-kit] <${name}> must be inside a <CartLine>`);
|
|
95
|
+
return s;
|
|
96
|
+
}
|
|
97
|
+
/** The line's quantity (`data-tq-quantity`): typed freely, saved on Enter or when it loses focus; 0 removes the line. */
|
|
98
|
+
export function CartLineQuantity({ className, labelClassName, label = 'hidden' }) {
|
|
99
|
+
const s = useLineOrThrow('CartLineQuantity');
|
|
100
|
+
const t = useTranslate();
|
|
101
|
+
const id = useId();
|
|
102
|
+
const [draft, setDraft] = useState(String(s.line.quantity));
|
|
103
|
+
const editing = useRef(false);
|
|
104
|
+
useEffect(() => {
|
|
105
|
+
if (!editing.current)
|
|
106
|
+
setDraft(String(s.line.quantity));
|
|
107
|
+
}, [s.line.quantity]);
|
|
108
|
+
const commit = () => {
|
|
109
|
+
editing.current = false;
|
|
110
|
+
const n = Math.floor(Number(draft));
|
|
111
|
+
if (!Number.isFinite(n) || n < 0) {
|
|
112
|
+
setDraft(String(s.line.quantity));
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (n !== s.line.quantity)
|
|
116
|
+
void (n === 0 ? s.remove() : s.setQuantity(n));
|
|
117
|
+
};
|
|
118
|
+
return (_jsxs(_Fragment, { children: [_jsx("label", { htmlFor: id, className: label === 'visible' ? labelClassName : cn(visuallyHidden(), labelClassName), children: t('cart.quantity') }), _jsx("input", { id: id, className: className, type: "number", inputMode: "numeric", min: 0, "data-tq-quantity": "", value: draft, "aria-busy": s.busy || undefined, onFocus: () => {
|
|
119
|
+
editing.current = true;
|
|
120
|
+
}, onChange: (e) => {
|
|
121
|
+
editing.current = true;
|
|
122
|
+
setDraft(e.target.value);
|
|
123
|
+
}, onBlur: commit, onKeyDown: (e) => {
|
|
124
|
+
if (e.key === 'Enter') {
|
|
125
|
+
e.preventDefault();
|
|
126
|
+
commit();
|
|
127
|
+
}
|
|
128
|
+
} })] }));
|
|
129
|
+
}
|
|
130
|
+
/** Quantity + 1 (`data-tq-action="cart:increment"`). Its accessible name is the "Increase quantity" string. */
|
|
131
|
+
export function CartLineIncrement({ children, disabled, ...rest }) {
|
|
132
|
+
const s = useLineOrThrow('CartLineIncrement');
|
|
133
|
+
const t = useTranslate();
|
|
134
|
+
return (_jsx("button", { ...rest, type: "button", "data-tq-action": "cart:increment", "aria-label": t('cart.increase'), disabled: disabled || s.busy, onClick: () => void s.increment(), children: children ?? '+' }));
|
|
135
|
+
}
|
|
136
|
+
/** Quantity − 1; at 1 it removes the line. Its accessible name is the "Decrease quantity" string. */
|
|
137
|
+
export function CartLineDecrement({ children, disabled, ...rest }) {
|
|
138
|
+
const s = useLineOrThrow('CartLineDecrement');
|
|
139
|
+
const t = useTranslate();
|
|
140
|
+
return (_jsx("button", { ...rest, type: "button", "data-tq-action": "cart:decrement", "aria-label": t('cart.decrease'), disabled: disabled || s.busy, onClick: () => void s.decrement(), children: children ?? '−' }));
|
|
141
|
+
}
|
|
142
|
+
/** Remove the line (`data-tq-action="cart:remove"`). */
|
|
143
|
+
export function CartLineRemove({ children, disabled, ...rest }) {
|
|
144
|
+
const s = useLineOrThrow('CartLineRemove');
|
|
145
|
+
const t = useTranslate();
|
|
146
|
+
return (_jsx("button", { ...rest, type: "button", "data-tq-action": "cart:remove", disabled: disabled || s.busy, onClick: () => void s.remove(), children: children ?? t('cart.remove') }));
|
|
147
|
+
}
|
|
148
|
+
/** The checkout link: exactly `Cart.checkoutUrl` (`data-tq-action="cart:checkout"`). Checkout never renders in a theme. */
|
|
149
|
+
export function CheckoutButton({ children, ...rest }) {
|
|
150
|
+
const cart = useCart();
|
|
151
|
+
const t = useTranslate();
|
|
152
|
+
return (_jsx("a", { ...rest, href: cart.checkoutUrl, "data-tq-action": "cart:checkout", "aria-disabled": cart.lines.length === 0 || undefined, children: children ?? t('cart.checkout') }));
|
|
153
|
+
}
|