Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

backstitch

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

backstitch

_To sew with overlapping stitches, to bind two pieces of fabric together._

  • 0.1.1
  • unpublished
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

backstitch! 🧵 CircleCI npm latest version

To sew with overlapping stitches, to bind two pieces of fabric together.

backstitch allows you to wrap your React UI elements in a Web Components custom elements API, and use them in places where it's otherwise tricky to embed React — e.g. in Elm!

API

backstitch.define

The backstitch.define function mimics the native customElements.define API:

backstitch.define("x-element", ReactComponent)

backstitch.props

backstitch.props serialises plain-old-javascript props to a JSON string under the correct data-props object key, so that they can be directly passed to a backstitch custom element.

With the right cocktail of polyfills, this can be used to check custom elements against their original tests, e.g.:

const container = render(
  React.createElement("x-element", backstitch.props(plainOldJavascriptProps))
)

backstitch.PROPS_ATTRIBUTE

The name of the props data-* attribute (i.e. "data-props").

React example

import * as backstitch from "backstitch"
import ReactButton from "./components/Button"

backstitch.define("x-button", ReactButton)

const ContrivedExample = props => (
  <x-button
    data-props=`{"size": "huge", "blink": ${props.isObnoxious}}`
    onClick={() => window.alert("Zap! Kapow!")}
  >
    Click Me!
  </x-button>
)

Elm example

import * as backstitch from "backstitch"
import ReactButton from "./components/Button"

backstitch.define("x-button", ReactButton)

const { Elm } = require("./App.elm")

Elm.Main.init({
  node: document.getElementById("root")
})
...

view : Model -> Html Msg
view model =
    let
        props =
            encode 0 <|
              object
                  [ ( "size", string "huge" )
                  , ( "blink" , bool model.isObnoxious )
                  ]
    in
    Html.node "x-button"
      [ attribute "data-props" props
      , onClick ZapKapow
      ]
      [ Html.text "Click Me!"
      ]

How it works (WIP)

On creation, the custom element creates a shadow DOM containing a single span element. It renders a bridging React component into this element, which hosts the wrapped component and supplies its props.

The custom element listens for changes on its data-props attribute. When it detects a change, it passes the attribute data to the bridge component, instructing it to update its state with the new data.

React then takes over and decides whether any of the wrapped components' props are affected by the new state in the bridge component. If so, this triggers a rerender of the wrapped component.

For more detail, see: ./packages/backstitch/src/custom-element.ts

Limitations

backstitch is only able to send props data which can be serialised to JSON. While in practice this covers many use cases, some React tricks such as passing event handlers or render functions as props will not work with custom elements.

FAQs

Package last updated on 31 Mar 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc