15.1.3 • Published 7 months ago

@shopify/polaris-viz-core v15.1.3

Weekly downloads
-
License
SEE LICENSE IN LI...
Repository
github
Last release
7 months 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>
  )
}
15.1.1

8 months ago

15.1.2

8 months ago

15.1.3

7 months ago

15.1.0

8 months ago

15.0.7

8 months ago

15.0.3

9 months ago

15.0.6

8 months ago

15.0.4

9 months ago

15.0.2

9 months ago

15.0.1

9 months ago

13.3.0

12 months ago

14.1.2

11 months ago

14.9.1

9 months ago

14.9.0

9 months ago

15.0.0

9 months ago

13.4.0

12 months ago

13.4.1

12 months ago

14.2.0

11 months ago

14.7.0

10 months ago

13.2.0

1 year ago

14.0.0

11 months ago

13.2.1

12 months ago

14.8.0

9 months ago

14.5.0

10 months ago

14.5.1

10 months ago

14.6.0

10 months ago

14.3.0

11 months ago

14.4.0

11 months ago

13.1.1

1 year ago

13.1.2

1 year ago

13.1.0

1 year ago

13.0.1

1 year ago

13.0.0

1 year ago

12.5.0

1 year ago

12.4.2

1 year ago

12.4.0

1 year ago

12.4.1

1 year ago

12.3.0

1 year ago

12.2.2

1 year ago

12.1.0

1 year ago

12.0.0

1 year ago

11.1.0

1 year ago

11.0.0

1 year ago

10.7.1

1 year ago

10.7.2

1 year ago

10.6.0

1 year ago

10.5.2

1 year ago

10.5.1

1 year ago

10.5.0

1 year ago

10.4.2

1 year ago

10.4.1

1 year ago

10.4.0-beta.1

1 year ago

10.4.0

1 year ago

10.3.2

1 year ago

10.3.1

1 year ago

10.3.0

1 year ago

10.2.0

1 year ago

10.2.1

1 year ago

9.8.1

2 years ago

9.8.0

2 years ago

10.0.0

2 years ago

10.0.1

2 years ago

9.16.0

2 years ago

9.12.0

2 years ago

9.9.0

2 years ago

9.5.1

2 years ago

9.5.0

2 years ago

9.15.0

2 years ago

9.11.0

2 years ago

9.6.2

2 years ago

9.6.1

2 years ago

9.6.0

2 years ago

9.4.1-beta.0

2 years ago

9.4.1-beta.2

2 years ago

9.10.6

2 years ago

9.10.7

2 years ago

9.18.0

2 years ago

9.18.1

2 years ago

9.10.2

2 years ago

9.14.0

2 years ago

9.10.4

2 years ago

9.10.5

2 years ago

9.10.0

2 years ago

9.10.1

2 years ago

9.7.0

2 years ago

10.1.0

2 years ago

9.18.2

2 years ago

9.17.1

2 years ago

9.13.0

2 years ago

9.4.0

2 years ago

9.3.6

2 years ago

9.3.5

2 years ago

9.3.4

2 years ago

9.3.3

2 years ago

9.3.2

2 years ago

9.3.1

2 years ago

9.3.0

2 years ago

7.16.2-y.0

2 years ago

7.16.2-y.1

2 years ago

7.16.2-y.2

2 years ago

7.16.2-y.3

2 years ago

7.16.2-y.4

2 years ago

7.16.2-y.5

2 years ago

8.2.0

2 years ago

9.0.1

2 years ago

9.0.0

2 years ago

9.2.2

2 years ago

9.2.1

2 years ago

9.1.1

2 years ago

9.1.0

2 years ago

9.2.0

2 years ago

8.0.4

2 years ago

8.1.1

2 years ago

8.0.1

2 years ago

8.0.0

2 years ago

8.0.3

2 years ago

8.0.2

2 years ago

7.13.1

2 years ago

7.15.0

2 years ago

7.11.1

3 years ago

7.13.0

2 years ago

7.16.0

2 years ago

7.16.1

2 years ago

7.14.0

2 years ago

7.14.1

2 years ago

7.12.0

2 years ago

7.8.0

3 years ago

7.4.4

3 years ago

7.6.0

3 years ago

7.4.2

3 years ago

7.4.1

3 years ago

7.8.1

3 years ago

7.4.5

3 years ago

7.11.0

3 years ago

7.4.0

3 years ago

7.7.1

3 years ago

7.7.0

3 years ago

7.5.1

3 years ago

7.9.0

3 years ago

7.10.0

3 years ago

7.3.1

3 years ago

7.3.0

3 years ago

7.1.0

3 years ago

7.2.0

3 years ago

7.3.2

3 years ago

6.3.0

3 years ago

6.5.0

3 years ago

3.0.0

3 years ago

6.6.1

3 years ago

6.6.0

3 years ago

4.0.0

3 years ago

5.0.0

3 years ago

6.0.1

3 years ago

6.0.0

3 years ago

6.0.3

3 years ago

6.2.0

3 years ago

6.0.2

3 years ago

6.4.0

3 years ago

7.0.0

3 years ago

2.1.0

3 years ago

4.1.1

3 years ago

1.10.3

3 years ago

1.11.1

3 years ago

1.10.2

3 years ago

1.9.1

3 years ago

1.9.0

3 years ago

1.8.0

3 years ago

1.7.1

3 years ago

1.6.0

3 years ago

1.5.0

3 years ago

2.0.0

3 years ago

1.9.3

3 years ago

1.9.2

3 years ago

1.11.0

3 years ago

1.10.0

3 years ago

1.2.0

3 years ago

1.4.0

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.1

3 years ago

1.1.0

3 years ago

1.0.2

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.1

3 years ago