@sonic-equipment/ui 201.0.0 → 202.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -27,8 +27,7 @@ const Button = forwardRef(({ _pseudo = 'none', 'aria-label': ariaLabel, children
27
27
  const children = (jsxs(Fragment, { children: [showIconOnLeft && iconElement, jsx("span", { className: buttonStyles.children, children: isLoading ? (isLoading === true ? _children : isLoading) : _children }), withArrow && (jsx(GlyphsArrowBoldCapsRightIcon, { className: buttonStyles['right-arrow-icon'] })), showIconOnRight && iconElement, isLoading && (jsx(ProgressCircle, { className: buttonStyles.spinner, size: "sm", variant: color === 'primary' ? 'white' : 'gray' }))] }));
28
28
  if (href) {
29
29
  // TODO: only button currently supports ref for now
30
- const Element = RouteLinkElement || 'a';
31
- return (jsx(Element, { "aria-label": ariaLabel, className: classNames, "data-disabled": isDisabled ? true : undefined, "data-test-selector": dataTestSelector, onClick: onClick, type: type, ...getRouteLinkProps(href, route), children: children }));
30
+ return (jsx(RouteLinkElement, { "aria-label": ariaLabel, className: classNames, "data-disabled": isDisabled ? true : undefined, "data-test-selector": dataTestSelector, onClick: onClick, type: type, ...getRouteLinkProps(href, route), children: children }));
32
31
  }
33
32
  return (jsx("button", { ref: ref, "aria-label": ariaLabel, className: classNames, "data-disabled": isDisabled ? true : undefined, "data-test-selector": dataTestSelector, disabled: isDisabled, form: form, formNoValidate: isValidating ? undefined : true, name: name, onClick: handleButtonClick, type: type, value: value, children: children }));
34
33
  });
@@ -13,8 +13,7 @@ function IconButton({ children, className: _className, color = 'primary', 'data-
13
13
  };
14
14
  const className = clsx(styles['icon-button'], styles[size], styles[color], _className);
15
15
  if (href) {
16
- const Element = RouteLinkElement || 'a';
17
- return (jsx(Element, { className: className, "data-disabled": isDisabled ? true : undefined, "data-test-selector": dataTestSelector, onClick: onClick, target: target, title: title, ...getRouteLinkProps(href, route), ...rest, children: children }));
16
+ return (jsx(RouteLinkElement, { className: className, "data-disabled": isDisabled ? true : undefined, "data-test-selector": dataTestSelector, onClick: onClick, target: target, title: title, ...getRouteLinkProps(href, route), ...rest, children: children }));
18
17
  }
19
18
  return (jsx("button", { className: className, "data-disabled": isDisabled ? true : undefined, "data-test-selector": dataTestSelector, disabled: isDisabled, onClick: handleButtonClick, title: title, type: type, ...rest, children: children }));
20
19
  }
@@ -12,8 +12,7 @@ const Link = forwardRef(({ children, className: _className, color, hasUnderline,
12
12
  [styles['has-underline']]: hasUnderline,
13
13
  }, styles['link'], color && styles[color], _className);
14
14
  if (href) {
15
- const Element = RouteLinkElement || 'a';
16
- return (jsx(Element, { ref: ref, className: className, "data-disabled": isDisabled ? true : undefined, id: id, onClick: onClick, onKeyUp: onKeyUp, role: role, tabIndex: isDisabled ? -1 : tabIndex, target: target, title: title, ...getRouteLinkProps(href, route), ...rest, children: children }));
15
+ return (jsx(RouteLinkElement, { ref: ref, className: className, "data-disabled": isDisabled ? true : undefined, id: id, onClick: onClick, onKeyUp: onKeyUp, role: role, tabIndex: isDisabled ? -1 : tabIndex, target: target, title: title, ...getRouteLinkProps(href, route), ...rest, children: children }));
17
16
  }
18
17
  return (jsx("button", { ref: ref, className: className, "data-disabled": isDisabled ? true : undefined, disabled: isDisabled ? true : undefined, id: id, onClick: onClick, onKeyUp: onKeyUp, role: role, tabIndex: isDisabled ? -1 : tabIndex, title: title, type: "button", ...rest, children: children }));
19
18
  });
package/dist/exports.d.ts CHANGED
@@ -404,6 +404,7 @@ export * from './shared/providers/cart-provider';
404
404
  export * from './shared/providers/favorite-provider';
405
405
  export * from './shared/providers/global-state-provider';
406
406
  export * from './shared/providers/react-query-container';
407
+ export * from './shared/routing/route-context';
407
408
  export * from './shared/routing/route-provider';
408
409
  export * from './shared/routing/route-utils';
409
410
  export * from './shared/routing/types';
@@ -413,6 +414,7 @@ export * from './shared/routing/use-on-navigate';
413
414
  export * from './shared/routing/use-paths';
414
415
  export * from './shared/routing/use-route-link';
415
416
  export * from './shared/routing/use-route-link-element';
417
+ export * from './shared/routing/use-router';
416
418
  export * from './shared/routing/with-routing';
417
419
  export * from './shared/utils/array';
418
420
  export * from './shared/utils/breakpoints';
package/dist/index.js CHANGED
@@ -404,6 +404,7 @@ export { CartProvider, useCartEvents } from './shared/providers/cart-provider.js
404
404
  export { FavoriteProvider, useFavorite, useFavoriteProduct } from './shared/providers/favorite-provider.js';
405
405
  export { GlobalStateProvider, GlobalStateProviderContext, useGlobalState } from './shared/providers/global-state-provider.js';
406
406
  export { ReactQueryContainer } from './shared/providers/react-query-container.js';
407
+ export { RouteContext } from './shared/routing/route-context.js';
407
408
  export { RouteProvider } from './shared/routing/route-provider.js';
408
409
  export { buildHref } from './shared/routing/route-utils.js';
409
410
  export { useLocation } from './shared/routing/use-location.js';
@@ -412,7 +413,8 @@ export { useOnNavigate } from './shared/routing/use-on-navigate.js';
412
413
  export { usePaths } from './shared/routing/use-paths.js';
413
414
  export { useRouteLink } from './shared/routing/use-route-link.js';
414
415
  export { useRouteLinkElement } from './shared/routing/use-route-link-element.js';
415
- export { withRouting } from './shared/routing/with-routing.js';
416
+ export { useRouter } from './shared/routing/use-router.js';
417
+ export { FallbackRouteLink, withRouting } from './shared/routing/with-routing.js';
416
418
  export { ensureArray } from './shared/utils/array.js';
417
419
  export { breakpoints, getCurrentBreakpoints } from './shared/utils/breakpoints.js';
418
420
  export { getCssPropertyValue } from './shared/utils/css.js';
@@ -0,0 +1,2 @@
1
+ import { RouteContextValue } from './types';
2
+ export declare const RouteContext: React.Context<RouteContextValue | undefined>;
@@ -0,0 +1,6 @@
1
+ "use client";
2
+ import { createContext } from 'react';
3
+
4
+ const RouteContext = createContext(undefined);
5
+
6
+ export { RouteContext };
@@ -1,17 +1,16 @@
1
1
  "use client";
2
- import { jsx, Fragment } from 'react/jsx-runtime';
3
- import { useGlobalState } from '../providers/global-state-provider.js';
4
- import { withRouting } from './with-routing.js';
2
+ import { jsx } from 'react/jsx-runtime';
3
+ import { useMemo } from 'react';
4
+ import { RouteContext } from './route-context.js';
5
5
 
6
- function RouteProvider({ children, Link = FallbackRouteLink, navigate, paths, url, }) {
7
- useGlobalState('routing', {
6
+ function RouteProvider({ children, Link, navigate, paths, url, }) {
7
+ const value = useMemo(() => ({
8
8
  Link,
9
9
  navigate,
10
10
  paths,
11
11
  url,
12
- });
13
- return jsx(Fragment, { children: children });
12
+ }), [Link, navigate, paths, url]);
13
+ return jsx(RouteContext.Provider, { value: value, children: children });
14
14
  }
15
- const FallbackRouteLink = withRouting('a');
16
15
 
17
16
  export { RouteProvider };
@@ -18,7 +18,7 @@ export interface Paths {
18
18
  REVIEW_AND_SUBMIT: string;
19
19
  SIGN_IN: string;
20
20
  }
21
- export interface RouteContext {
21
+ export interface RouteContextValue {
22
22
  Link?: ElementType<RouteLinkElementProps>;
23
23
  navigate: NavigateFunction;
24
24
  paths: Paths;
@@ -1,9 +1,9 @@
1
1
  import qs from 'query-string';
2
2
  export declare function useLocation(): {
3
3
  basePathname: string | undefined;
4
- fullPathname: string | undefined;
4
+ fullPathname: string;
5
5
  href: string;
6
- pathname: string | undefined;
6
+ pathname: string;
7
7
  query: qs.ParsedQuery<string>;
8
- search: string | undefined;
8
+ search: string;
9
9
  };
@@ -1,15 +1,15 @@
1
+ "use client";
1
2
  import qs from 'query-string';
2
- import { useGlobalState } from '../providers/global-state-provider.js';
3
+ import { useRouter } from './use-router.js';
3
4
 
4
5
  function useLocation() {
5
- const [routeState] = useGlobalState('routing');
6
- const { url: { basePathname, pathname, search }, } = routeState || { url: {} };
6
+ const { url: { basePathname, pathname, search }, } = useRouter();
7
7
  const query = qs.parse(search || '');
8
8
  return {
9
9
  basePathname,
10
10
  fullPathname: pathname,
11
11
  href: `${pathname}${search}`,
12
- pathname: pathname?.replace(new RegExp(`^${basePathname}`), ''),
12
+ pathname: pathname.replace(new RegExp(`^${basePathname}`), ''),
13
13
  query,
14
14
  search,
15
15
  };
@@ -1,22 +1,19 @@
1
1
  "use client";
2
2
  import { useRef, useCallback } from 'react';
3
- import { useGlobalState } from '../providers/global-state-provider.js';
4
3
  import { useOnNavigate } from './use-on-navigate.js';
4
+ import { useRouter } from './use-router.js';
5
5
 
6
6
  function useNavigate() {
7
7
  const isNavigatingRef = useRef(false);
8
- const [state] = useGlobalState('routing');
8
+ const routeState = useRouter();
9
9
  const trigger = useOnNavigate(() => {
10
10
  isNavigatingRef.current = false;
11
11
  });
12
- if (!state) {
13
- throw new Error('RouteProvider not found');
14
- }
15
12
  const navigate = useCallback((href, options) => {
16
13
  trigger(href, options);
17
14
  isNavigatingRef.current = true;
18
- return state.navigate(href, options);
19
- }, [state, trigger]);
15
+ return routeState.navigate(href, options);
16
+ }, [routeState, trigger]);
20
17
  return { isNavigating: isNavigatingRef.current, navigate };
21
18
  }
22
19
 
@@ -1,10 +1,9 @@
1
- import { useGlobalState } from '../providers/global-state-provider.js';
1
+ "use client";
2
+ import { useRouter } from './use-router.js';
2
3
 
3
4
  function usePaths() {
4
- const [routeState] = useGlobalState('routing');
5
- if (!routeState)
6
- throw new Error('usePaths must be used within a RouteProvider context');
7
- return routeState.paths;
5
+ const { paths } = useRouter();
6
+ return paths;
8
7
  }
9
8
 
10
9
  export { usePaths };
@@ -1 +1 @@
1
- export declare function useRouteLinkElement(): React.ElementType<import("./types").RouteLinkElementProps> | undefined;
1
+ export declare function useRouteLinkElement(): React.ElementType<import("./types").RouteLinkElementProps>;
@@ -1,11 +1,10 @@
1
- import { useGlobalState } from '../providers/global-state-provider.js';
1
+ "use client";
2
+ import { useRouter } from './use-router.js';
3
+ import { FallbackRouteLink } from './with-routing.js';
2
4
 
3
5
  function useRouteLinkElement() {
4
- const [state] = useGlobalState('routing');
5
- if (!state) {
6
- throw new Error('RouteProvider not found');
7
- }
8
- return state.Link;
6
+ const { Link } = useRouter();
7
+ return Link || FallbackRouteLink;
9
8
  }
10
9
 
11
10
  export { useRouteLinkElement };
@@ -1,6 +1,6 @@
1
1
  import { NavigateOptions } from './types';
2
2
  export declare function useRouteLink(): {
3
- RouteLinkElement: React.ElementType<import("./types").RouteLinkElementProps> | undefined;
3
+ RouteLinkElement: React.ElementType<import("./types").RouteLinkElementProps>;
4
4
  getRouteLinkProps: (href: string, route?: NavigateOptions) => {
5
5
  href: string;
6
6
  onNavigate: () => void;
@@ -1,3 +1,4 @@
1
+ "use client";
1
2
  import { useCallback } from 'react';
2
3
  import { buildHref } from './route-utils.js';
3
4
  import { useLocation } from './use-location.js';
@@ -0,0 +1 @@
1
+ export declare function useRouter(): import("./types").RouteContextValue;
@@ -0,0 +1,13 @@
1
+ "use client";
2
+ import { useContext } from 'react';
3
+ import { RouteContext } from './route-context.js';
4
+
5
+ function useRouter() {
6
+ const context = useContext(RouteContext);
7
+ if (!context) {
8
+ throw new Error('useRouter must be used within a RouteProvider');
9
+ }
10
+ return context;
11
+ }
12
+
13
+ export { useRouter };
@@ -5,3 +5,4 @@ export interface WithRoutingProps {
5
5
  route?: NavigateOptions;
6
6
  }
7
7
  export declare function withRouting(component: ElementType<RouteLinkElementProps>): React.FunctionComponent<RouteLinkElementProps>;
8
+ export declare const FallbackRouteLink: React.FunctionComponent<RouteLinkElementProps>;
@@ -29,5 +29,6 @@ function withRouting(component) {
29
29
  Component.displayName = `WithRouting(${component.toString()})`;
30
30
  return Component;
31
31
  }
32
+ const FallbackRouteLink = withRouting('a');
32
33
 
33
- export { withRouting };
34
+ export { FallbackRouteLink, withRouting };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sonic-equipment/ui",
3
- "version": "201.0.0",
3
+ "version": "202.0.0",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "engines": {