npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@shopify/polaris-viz-core

v15.3.2

Published

Core logic for Polaris Viz that is not platform specific

Downloads

268,276

Readme

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