@asteby/storefront-react 0.2.0 → 0.3.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/dist/index.js CHANGED
@@ -1,41 +1,10 @@
1
- import { createContext, useMemo, useContext, useRef, useState, useCallback, useEffect, createElement, Fragment as Fragment$1, useId } from 'react';
2
- import { createStorefrontClient, formatMoney, parseMoney, isStorefrontError } from '@asteby/storefront';
3
- import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
1
+ import { useOptionalStorefront } from './chunk-C6QZNKRE.js';
2
+ export { DEFAULT_IMAGE_WIDTHS, Image, Money, ProductPrice, StorefrontProvider, defaultImageLoader, useOptionalStorefront, useStorefront, useStorefrontClient } from './chunk-C6QZNKRE.js';
3
+ import { createContext, useRef, useState, useCallback, useEffect, useMemo, useContext, Fragment as Fragment$1, useId } from 'react';
4
+ import { formatMoney, isStorefrontError } from '@asteby/storefront';
5
+ import { jsx, Fragment, jsxs } from 'react/jsx-runtime';
4
6
  import { toSchemaOpeningHours, isValidPoint, rankByDistance, openStatus, directionsUrl } from '@asteby/storefront/geo';
5
7
 
6
- // src/context.tsx
7
- var StorefrontContext = createContext(null);
8
- function StorefrontProvider({ client, config, shop = null, locale, currency, children }) {
9
- const configKey = config ? `${config.baseUrl}|${config.token}|${config.locale ?? ""}|${config.customerToken ?? ""}` : "";
10
- const resolvedClient = useMemo(
11
- () => client ?? (config ? createStorefrontClient(config) : null),
12
- [client, configKey]
13
- );
14
- const value = useMemo(
15
- () => ({
16
- client: resolvedClient,
17
- shop,
18
- locale: locale ?? config?.locale ?? shop?.locale ?? "es-MX",
19
- currency: currency ?? shop?.currency_code ?? "MXN"
20
- }),
21
- [resolvedClient, shop, locale, currency, config?.locale]
22
- );
23
- return /* @__PURE__ */ jsx(StorefrontContext.Provider, { value, children });
24
- }
25
- function useStorefront() {
26
- const ctx = useContext(StorefrontContext);
27
- if (!ctx) throw new Error("useStorefront must be used inside <StorefrontProvider>");
28
- return ctx;
29
- }
30
- function useOptionalStorefront() {
31
- return useContext(StorefrontContext);
32
- }
33
- function useStorefrontClient() {
34
- const { client } = useStorefront();
35
- if (!client) throw new Error("<StorefrontProvider> needs a `client` or `config` for browser-side calls");
36
- return client;
37
- }
38
-
39
8
  // src/cookie.ts
40
9
  var CART_COOKIE_NAME = "sf_cart";
41
10
  function getCartIdFromCookie(cookieHeader, name = CART_COOKIE_NAME) {
@@ -314,128 +283,6 @@ function useCart() {
314
283
  function useOptionalCart() {
315
284
  return useContext(CartContext);
316
285
  }
317
- function Money({
318
- data,
319
- as,
320
- locale,
321
- withoutTrailingZeros,
322
- withoutCurrency,
323
- currencyDisplay,
324
- ...rest
325
- }) {
326
- const sf = useOptionalStorefront();
327
- const text = formatMoney(data, locale ?? sf?.locale, {
328
- ...withoutTrailingZeros !== void 0 ? { withoutTrailingZeros } : {},
329
- ...withoutCurrency !== void 0 ? { withoutCurrency } : {},
330
- ...currencyDisplay !== void 0 ? { currencyDisplay } : {}
331
- });
332
- return createElement(as ?? "span", rest, text);
333
- }
334
- var DEFAULT_IMAGE_WIDTHS = [320, 480, 640, 768, 1024, 1280, 1600];
335
- var defaultImageLoader = (src, width, height) => {
336
- try {
337
- const isAbsolute = /^[a-z][a-z0-9+.-]*:/i.test(src);
338
- const url = new URL(src, "http://relative.invalid");
339
- url.searchParams.set("width", String(width));
340
- if (height) url.searchParams.set("height", String(height));
341
- return isAbsolute ? url.toString() : url.pathname + url.search + url.hash;
342
- } catch {
343
- return src;
344
- }
345
- };
346
- function parseRatio(ratio) {
347
- if (!ratio) return null;
348
- const [w, h] = ratio.split(/[/:]/).map((n) => Number.parseFloat(n));
349
- return w && h ? w / h : null;
350
- }
351
- function Image({
352
- data,
353
- sizes = "100vw",
354
- widths = DEFAULT_IMAGE_WIDTHS,
355
- loader = defaultImageLoader,
356
- priority = false,
357
- aspectRatio,
358
- alt,
359
- loading,
360
- decoding,
361
- ...rest
362
- }) {
363
- const ratio = parseRatio(aspectRatio) ?? (data.width && data.height ? data.width / data.height : null);
364
- let candidates = [...widths].sort((a, b) => a - b);
365
- if (data.width) {
366
- const capped = candidates.filter((w) => w <= data.width);
367
- candidates = capped.length ? capped : [data.width];
368
- }
369
- const srcSet = loader ? candidates.map((w) => `${loader(data.url, w, ratio ? Math.round(w / ratio) : void 0)} ${w}w`).join(", ") : void 0;
370
- const width = data.width ?? candidates[candidates.length - 1];
371
- const height = data.height ?? (ratio && width ? Math.round(width / ratio) : void 0);
372
- return /* @__PURE__ */ jsx(
373
- "img",
374
- {
375
- src: data.url,
376
- alt: alt ?? data.alt ?? "",
377
- ...srcSet ? { srcSet, sizes } : {},
378
- ...width ? { width } : {},
379
- ...height ? { height } : {},
380
- loading: loading ?? (priority ? "eager" : "lazy"),
381
- decoding: decoding ?? (priority ? "auto" : "async"),
382
- ...priority ? { fetchPriority: "high" } : {},
383
- ...rest
384
- }
385
- );
386
- }
387
- var VISUALLY_HIDDEN = {
388
- position: "absolute",
389
- width: 1,
390
- height: 1,
391
- padding: 0,
392
- margin: -1,
393
- overflow: "hidden",
394
- clip: "rect(0, 0, 0, 0)",
395
- whiteSpace: "nowrap",
396
- border: 0
397
- };
398
- function ProductPrice({
399
- product,
400
- variant,
401
- fromLabel = "Desde",
402
- compareAtLabel = "Precio anterior",
403
- locale,
404
- withoutTrailingZeros,
405
- ...rest
406
- }) {
407
- let price = null;
408
- let compareAt = null;
409
- let isRange = false;
410
- if (variant) {
411
- price = variant.price;
412
- compareAt = variant.compare_at_price;
413
- } else if (product) {
414
- price = product.price_range.min;
415
- compareAt = product.compare_at_price_range?.min ?? null;
416
- isRange = product.price_range.min.amount !== product.price_range.max.amount;
417
- }
418
- if (!price) return null;
419
- const onSale = compareAt !== null && parseMoney(compareAt) > parseMoney(price);
420
- const opts = { ...locale ? { locale } : {}, ...withoutTrailingZeros !== void 0 ? { withoutTrailingZeros } : {} };
421
- return /* @__PURE__ */ jsxs("div", { "data-on-sale": onSale || void 0, ...rest, children: [
422
- isRange ? /* @__PURE__ */ jsxs("span", { "data-price-prefix": "", children: [
423
- fromLabel,
424
- " "
425
- ] }) : null,
426
- /* @__PURE__ */ jsx(Money, { data: price, "data-price": "", ...opts }),
427
- onSale && compareAt ? /* @__PURE__ */ jsxs(Fragment, { children: [
428
- " ",
429
- /* @__PURE__ */ jsxs("s", { "data-compare-at-price": "", children: [
430
- /* @__PURE__ */ jsxs("span", { style: VISUALLY_HIDDEN, children: [
431
- compareAtLabel,
432
- ": "
433
- ] }),
434
- /* @__PURE__ */ jsx(Money, { data: compareAt, ...opts })
435
- ] })
436
- ] }) : null
437
- ] });
438
- }
439
286
  function toParams(input) {
440
287
  if (!input) return new URLSearchParams();
441
288
  return input instanceof URLSearchParams ? new URLSearchParams(input) : new URLSearchParams(input);
@@ -1336,6 +1183,6 @@ function NearestLocationForm({
1336
1183
  );
1337
1184
  }
1338
1185
 
1339
- export { AddToCartButton, CART_COOKIE_NAME, CartProvider, DEFAULT_IMAGE_WIDTHS, DirectionsLink, Image, JsonLd, LocationList, LocationPicker, Money, NearestLocationForm, OpenStatus, PickupOption, ProductPrice, SELECTED_LOCATION_PARAM, SELECTED_LOCATION_STORAGE_KEY, SectionRegistry, Sections, StorefrontProvider, ThemeStyle, VariantSelector, breadcrumbJsonLd, collectionJsonLd, collectionMeta, createSectionRegistry, defaultImageLoader, defineSection, faqJsonLd, getCartIdFromCookie, isSafeCssValue, localBusinessJsonLd, openingHoursJsonLd, optimisticCart, organizationJsonLd, productJsonLd, productMeta, readCartCookie, readSelectedLocationId, resolveSectionProps, sectionJsonSchema, seoMeta, serializeCartCookie, serializeJsonLd, themeFontsHref, themeToCss, themeToCssVariables, toMerchandise, tokenName, useCart, useGeolocation, useNearestLocations, useOptionalCart, useOptionalStorefront, useProductOptions, useSelectedLocation, useStorefront, useStorefrontClient, writeCartCookie };
1186
+ export { AddToCartButton, CART_COOKIE_NAME, CartProvider, DirectionsLink, JsonLd, LocationList, LocationPicker, NearestLocationForm, OpenStatus, PickupOption, SELECTED_LOCATION_PARAM, SELECTED_LOCATION_STORAGE_KEY, SectionRegistry, Sections, ThemeStyle, VariantSelector, breadcrumbJsonLd, collectionJsonLd, collectionMeta, createSectionRegistry, defineSection, faqJsonLd, getCartIdFromCookie, isSafeCssValue, localBusinessJsonLd, openingHoursJsonLd, optimisticCart, organizationJsonLd, productJsonLd, productMeta, readCartCookie, readSelectedLocationId, resolveSectionProps, sectionJsonSchema, seoMeta, serializeCartCookie, serializeJsonLd, themeFontsHref, themeToCss, themeToCssVariables, toMerchandise, tokenName, useCart, useGeolocation, useNearestLocations, useOptionalCart, useProductOptions, useSelectedLocation, writeCartCookie };
1340
1187
  //# sourceMappingURL=index.js.map
1341
1188
  //# sourceMappingURL=index.js.map