13.1.0 • Published 8 days ago

@shopify/polaris-viz-core v13.1.0

Weekly downloads
-
License
SEE LICENSE IN LI...
Repository
github
Last release
8 days ago

🧠 polaris-viz-core

packages/polaris-viz-core contains platform agnostic utility functions, hooks, constants, types and UI components.

It gets published as the @shopify/polaris-viz-core library that is used by both @shopify/polaris-viz and @shopify/polaris-viz-native

👯‍♀️ Sharing code between React and React Native

Keeping the bundle size small

To keep the bundle size of the libraries small, @shopify/polaris-viz-core shouldn't depend on platform specific packages, like @react-native or @react-spring/web, directly.

Sharing UI components

To render SVG tags in React Native, we use the react-native-svg library. @shopify/polaris-viz doesn't need this package as a dependency though, since its targets are web browsers only. If we added react-native-svg as a depency of @shopify/polaris-viz-core we would also increase @shopify/polaris-viz bundle size for something that only @shopify/polaris-viz-native needs.

To solve this, we:

  • Store all regular SVG tags as React components in the polaris-viz-context used by PolarisVizProvider:
// packages/polaris-viz-core/polaris-viz-context.ts

export const PolarisVizContext = createContext({
  components: {
    Svg: ({children, ...props}) => createElement('svg', props, children),
    Circle: ({children, ...props}) => createElement('circle', props, children),
    Ellipse: ({children, ...props}) => createElement('ellipse', props, children),
    G: ({children, ...props}) => createElement('g', props, children),
    Text: ({children, ...props}) => createElement('text', props, children),
    // ... other SVG tags
  },
});
  • Re-export PolarisVizProvider from polaris-viz-native overwriting the regular SVG tags, with the equivalent tags from react-native-svg
// packages/polaris-viz-native/PolarisVizProvider.tsx

import {PolarisVizProvider as OriginalPolarisVizProvider} from '@shopify/polaris-viz-core';

import  {
  Svg,
  Circle,
  // ...
} from 'react-native-svg';

export const NativeComponents = {
  Svg,
  Circle,
  // ...
}

export const PolarisVizProvider = ({themes, children}) => {
  return (
    <OriginalPolarisVizProvider
      themes={themes}
      components={NativeComponents}
      animated={animated}
    >
      {children}
    </OriginalPolarisVizProvider>
  );
};
  • When building UI components in polaris-viz-core, instead of using <svg> directly, we get <Svg> from the context:
// packages/polaris-viz-core/SomeSharedComponent.tsx

export function SomeSharedComponent() {
  const {
    components: {Svg, Rect},
  } = usePolarisVizContext();

  return (
    <Svg>
      <Rect />
    </Svg>
  )
}

With these changes in place,

  • in polaris-viz: <Svg> and <Rect> will fetch the svg and rect tags from the default values of polaris-viz-context and render correctly in web browsers
  • in polaris-viz-native: <Svg> and <Rect> will fetch the Svg and Rect tags from the react-native-svg library that were used to overwrite the default tags in the PolarisVizProvider, thus rendering correctly in React Native

To summarize:

graph demontrating that core components fetching svg tags from PolarisVizProvider will use regular SVG tags in polaris-viz and native SVG tags in polaris-viz-native

We use react-spring to handle animations. This library also has platform specific exports to keep bundle size small: @react-spring/web and @react-spring/native

To animate components in core, we fetch the platform specific animated function from the PolarisVizContext, similarly to how we get the correct SVG tags.

// packages/polaris-viz-core/SomeSharedComponent.tsx

import { useSpring } from "@react-spring/core"

export function SomeSharedComponent() {
  const {
    components: {Svg, Circle},
    animated,
  } = usePolarisVizContext();

  const {animatedRadius} = useSpring({
    from: {
      animatedRadius: 0,
    },
    to: {
      animatedRadius: 100,
    },
  });

  const AnimatedCircle = animated(Circle);


  return (
    <Svg>
      <AnimatedCircle radius={animatedRadius} />
    </Svg>
  )
}
13.1.0

8 days ago

13.0.1

15 days ago

13.0.0

16 days ago

12.5.0

27 days ago

12.4.2

28 days ago

12.4.0

28 days ago

12.4.1

28 days ago

12.3.0

29 days ago

12.2.2

1 month ago

12.1.0

1 month ago

12.0.0

1 month ago

11.1.0

1 month ago

11.0.0

2 months ago

10.7.1

2 months ago

10.7.2

2 months ago

10.6.0

2 months ago

10.5.2

2 months ago

10.5.1

2 months ago

10.5.0

3 months ago

10.4.2

3 months ago

10.4.1

3 months ago

10.4.0-beta.1

3 months ago

10.4.0

3 months ago

10.3.2

3 months ago

10.3.1

4 months ago

10.3.0

4 months ago

10.2.0

4 months ago

10.2.1

4 months ago

9.8.1

10 months ago

9.8.0

10 months ago

10.0.0

6 months ago

10.0.1

6 months ago

9.16.0

7 months ago

9.12.0

8 months ago

9.9.0

9 months ago

9.5.1

10 months ago

9.5.0

10 months ago

9.15.0

7 months ago

9.11.0

8 months ago

9.6.2

10 months ago

9.6.1

10 months ago

9.6.0

10 months ago

9.4.1-beta.0

10 months ago

9.4.1-beta.2

10 months ago

9.10.6

8 months ago

9.10.7

8 months ago

9.18.0

6 months ago

9.18.1

6 months ago

9.10.2

9 months ago

9.14.0

7 months ago

9.10.4

9 months ago

9.10.5

8 months ago

9.10.0

9 months ago

9.10.1

9 months ago

9.7.0

10 months ago

10.1.0

5 months ago

9.18.2

6 months ago

9.17.1

6 months ago

9.13.0

7 months ago

9.4.0

11 months ago

9.3.6

11 months ago

9.3.5

11 months ago

9.3.4

11 months ago

9.3.3

11 months ago

9.3.2

11 months ago

9.3.1

11 months ago

9.3.0

12 months ago

7.16.2-y.0

1 year ago

7.16.2-y.1

1 year ago

7.16.2-y.2

1 year ago

7.16.2-y.3

1 year ago

7.16.2-y.4

1 year ago

7.16.2-y.5

1 year ago

8.2.0

1 year ago

9.0.1

1 year ago

9.0.0

1 year ago

9.2.2

1 year ago

9.2.1

1 year ago

9.1.1

1 year ago

9.1.0

1 year ago

9.2.0

1 year ago

8.0.4

1 year ago

8.1.1

1 year ago

8.0.1

1 year ago

8.0.0

1 year ago

8.0.3

1 year ago

8.0.2

1 year ago

7.13.1

1 year ago

7.15.0

1 year ago

7.11.1

1 year ago

7.13.0

1 year ago

7.16.0

1 year ago

7.16.1

1 year ago

7.14.0

1 year ago

7.14.1

1 year ago

7.12.0

1 year ago

7.8.0

1 year ago

7.4.4

2 years ago

7.6.0

2 years ago

7.4.2

2 years ago

7.4.1

2 years ago

7.8.1

1 year ago

7.4.5

2 years ago

7.11.0

1 year ago

7.4.0

2 years ago

7.7.1

1 year ago

7.7.0

1 year ago

7.5.1

2 years ago

7.9.0

1 year ago

7.10.0

1 year ago

7.3.1

2 years ago

7.3.0

2 years ago

7.1.0

2 years ago

7.2.0

2 years ago

7.3.2

2 years ago

6.3.0

2 years ago

6.5.0

2 years ago

3.0.0

2 years ago

6.6.1

2 years ago

6.6.0

2 years ago

4.0.0

2 years ago

5.0.0

2 years ago

6.0.1

2 years ago

6.0.0

2 years ago

6.0.3

2 years ago

6.2.0

2 years ago

6.0.2

2 years ago

6.4.0

2 years ago

7.0.0

2 years ago

2.1.0

2 years ago

4.1.1

2 years ago

1.10.3

2 years ago

1.11.1

2 years ago

1.10.2

2 years ago

1.9.1

2 years ago

1.9.0

2 years ago

1.8.0

2 years ago

1.7.1

2 years ago

1.6.0

2 years ago

1.5.0

2 years ago

2.0.0

2 years ago

1.9.3

2 years ago

1.9.2

2 years ago

1.11.0

2 years ago

1.10.0

2 years ago

1.2.0

2 years ago

1.4.0

2 years ago

1.3.1

2 years ago

1.3.0

2 years ago

1.2.1

2 years ago

1.1.0

2 years ago

1.0.2

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.1

2 years ago