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

@justeattakeaway/pie-wrapper-react

v0.14.2

Published

Common react wrapper for use for PIE custom elements

Downloads

1,562

Readme

pie-wrapper-react

Required dependencies

In order to use this package, your component must have the following devDependencies:

yarn add -D @custom-elements-manifest/analyzer \
@justeattakeaway/pie-components-config \
@justeattakeaway/pie-wrapper-react \
cem-plugin-module-file-extensions

Required npm scripts

create:manifest

This package assumes that a custom-elements.json file exists in the components root directory. This can be generated by defining a new script in your component package.json.

"create:manifest": "yarn cem analyze --litelement",

This script utilises the @custom-elements-manifest/analyzer dependency to search through the component src for any custom elements and condenses it's information, such as events and attributes, into a large json object - making it easier to use for wrappers. Therefore, any changes made to a web component will automatically be reflected in the custom-elements.json file during the build process.

Changes to this file should be committed as part of your PR. These are necessary for the react-wrapper to be generated with the most up to date version of the component so should be committed along with your other file changes.

build:react-wrapper

In order to create the react wrapper, this package exposes a build-react-wrapper executable that can be added to your component package.json:

"build:react-wrapper": "npx build-react-wrapper"

Script execution order

These scripts should be executed before your main build script. If your repo uses Turborepo, you can define these tasks as dependencies of your build task in turbo.json like so:

{
  "$schema": "https://turborepo.org/schema.json",
  "pipeline": {
    "build": {
      "dependsOn": [
        "^build",
        "build:react-wrapper"
      ],
      "outputs": [
        "dist/**"
      ]
    },
    "build:react-wrapper": {
        "dependsOn": [
            "create:manifest"
        ],
      "outputs": [
        "src/react.ts"
      ]
    },
    "create:manifest": {
      "outputs": [
        "custom-elements.json"
      ]
    },

Alternatively, simply modify your build script in package.json:

"build": "yarn create:manifest && yarn build:react-wrapper && <existing-build-command>",
"build:react-wrapper": "npx build-react-wrapper",
"create:manifest": "yarn cem analyze --litelement",

Required configuration

package.json In order to allow tools to find npm packages with custom element manifests without having to download package tarballs, packages should have a "customElements" field in their package.json that points to the manifest and should also be included as part of your published package:

"files": [
    ...
    "custom-elements.json"
],
"customElements": "custom-elements.json"

custom-elements-manifest.config.js

You should also create custom-elements-manifest.config.js in the root of your component directory. At a minimum, this configuration should extend the shared config from @justeattakeaway/pie-components-config.

import customElementsManifestConfig from '@justeattakeaway/pie-components-config/custom-elements-manifest.config.js';

export default customElementsManifestConfig;

Usage

This package is for generating a React wrapper during the build process of a Lit component. Previously, React developers would need to install the component, then manually wrap it within a createComponent function before it could be used. This is because React 18 and previous versions don't handle web components and custom elements out of the box correctly, due to how React treats custom props and events.

With this package, the below code (example of pie-button) is automatically generated into a react.ts file within the component's src directory. The wrapper then gets saved to the component's dist folder during the build.

import * as React from 'react';
import { createComponent } from '@lit/react';
import { PieButton as PieButtonLit } from './index';
import { type ButtonProps } from './defs';

const PieButtonReact = createComponent({
        displayName: 'PieButton',
        elementClass: PieButtonLit,
        react: React,
        tagName: 'pie-button',
        events: {}
    });

// Provides missing contextual types for a React button component
// Use the React IntrinsicElements interface as reference for mapping standard HTML elements to existing React Interfaces
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/0bb210867d16170c4a08d9ce5d132817651a0f80/types/react/index.d.ts#L2829
type ReactBaseType = React.ButtonHTMLAttributes<HTMLButtonElement>;

// Workaround for `createComponent` setting all props set as optional with the additional contextual types declared above
export const PieButton = PieButtonReact as React.ForwardRefExoticComponent<React.PropsWithoutRef<ButtonProps> & React.RefAttributes<PieButtonLit> & ReactBaseType>;

To use the React wrapper in an application, import Pie{Component} from the package of the component, with the additional import of dist/react. For example:

import { PieButton } from '@justeattakeaway/pie-button/dist/react'

When constructing your web component, please reference the componentSelector variable and component class to define your custom element to have consistent identifiers across the component. For example:

customElements.define(componentSelector, PieIconButton);

Alternatively, you can define your custom element with a string and component class directly. For example

customElements.define('pie-icon-button', PieIconButton);

The first parameter must be a reference to componentSelector or a string - this will be used to construct the tag name in the react component.

Note: In order for the custom-elements-manifest/analyzer to recognise events inside your component, please declare the event inside the this.dispatchEvent function. For example:

this.dispatchEvent(new CustomEvent('CustomEvent', { detail: 'WC event dispatched' }))

Credits

This package was heavily inspired by spectrum-web-components.