# @shopify/polaris-viz-core

> Core logic for Polaris Viz that is not platform specific

Latest version **16.16.0** (published 2025-05-19) · SEE LICENSE IN LICENSE.md license · 0 weekly downloads

## Install

```sh
npm install @shopify/polaris-viz-core
pnpm add @shopify/polaris-viz-core
yarn add @shopify/polaris-viz-core
bun add @shopify/polaris-viz-core
```

## Health

**Score 45/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; has provenance; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 16.16.0 |
| Published | 2025-05-19 |
| First published | 2022-03-01 |
| Weekly downloads | 0 |
| License | SEE LICENSE IN LICENSE.md |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 7 |
| Unpacked size | 429.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (Unknown) |
| GitHub stars | 365 |
| Author | Shopify Inc. |
| Maintainers | shopify-admin, shopify-dep, mishsmelle, netlohan, pmoloney89, maryharte, jaimie.rockburn, jaykay101 |

## Links

- npm: https://www.npmjs.com/package/@shopify/polaris-viz-core
- Repository: https://github.com/Shopify/polaris-viz
- Homepage: https://github.com/Shopify/polaris-viz#readme
- Issues: https://github.com/Shopify/polaris-viz/issues
- npm.io page: https://npm.io/package/@shopify/polaris-viz-core

## Dependencies (7)

- [d3-path](https://npm.io/package/d3-path.md) ^3.0.1
- [d3-array](https://npm.io/package/d3-array.md) ^3.2.0
- [d3-color](https://npm.io/package/d3-color.md) ^3.1.0
- [d3-scale](https://npm.io/package/d3-scale.md) ^4.0.2
- [d3-shape](https://npm.io/package/d3-shape.md) ^3.1.0
- [@react-spring/core](https://npm.io/package/@react-spring/core.md) ^9.4.5
- [@react-spring/animated](https://npm.io/package/@react-spring/animated.md) ^9.4.5

## Recent versions

- 16.16.0 (latest) — 2025-05-19
- 15.3.2-tooltips-fix-grid-beta.2 (beta) — 2024-11-20
- 16.15.6 — 2025-05-12
- 16.15.5 — 2025-05-09
- 16.15.4 — 2025-05-08
- 16.15.3 — 2025-05-07
- 16.15.2 — 2025-05-02
- 16.15.1 — 2025-05-01
- 16.15.0 — 2025-04-29
- 16.14.0 — 2025-04-25
- 16.13.0 — 2025-04-22
- 16.12.1 — 2025-04-16
- 16.12.0 — 2025-03-27
- 16.11.0 — 2025-03-10
- 16.10.1 — 2025-03-05
- … 244 more at https://npm.io/package/@shopify/polaris-viz-core/versions

## README

<br/>

## 🧠 `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`


<br/>
<hr/>
<br/>

## 👯‍♀️ Sharing code between React and React Native

<br/>
<br/>

### 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.

<br/>
<br/>

### Sharing UI components

To render SVG tags in React Native, we use [the `react-native-svg` library](https://github.com/react-native-svg/react-native-svg). `@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`:

```ts
// 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`

```ts
// 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:

```ts
// 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`:
  <br/>
  `<Svg>` and `<Rect>` will fetch the `svg` and `rect` tags from the default values of `polaris-viz-context` and render correctly in web browsers
  <br/>
  <br/>
- in `polaris-viz-native`:
  <br/>
  `<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](../../public/PolarisVizProvider_graph.png)

<br/>
<hr/>
<br/>
### Animating shared UI components

We use [react-spring](https://react-spring.io/) 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.

```tsx

// 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>
  )
}

```

---
_Source: https://npm.io/package/@shopify/polaris-viz-core · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
