1.2.0 • Published 1 year ago

use-shadow-dom v1.2.0

Weekly downloads
-
License
-
Repository
github
Last release
1 year ago

useShadow

npm version npm downloads

Preview

online demo

Install

npm i use-shadow-dom

Import

import useShadow from 'use-shadow-dom'

Usage

function App() {
  // this element will be rendered in shadow dom.
  // and element will never rerender
  const element = useShadow(<MyComponent />)

  return (
    <div className="App">
      {element}
    </div>
  )
}

function MyComponent() {
  return <div>Hello world</div>
}

If you pass some props(eg. your custom css) to your component without do anything, the component will not rerender.

const element = useShadow(<MyComponent count={count} />, [], {
  styleContent: '...' // element will not rerender even though count changed.
})

The component just like be memorized. you can tell useShadow your dependencies by pass dependencyList as second params:

const element = useShadow(<MyComponent count={count} />, [count])

When count changed, useShadow will return a new element.

Hook API

ValueTypeDefaultDescription
ComponentReactNode-The component you want to attach to shadow dom.
depsDependencyList[]The dependencies that control component updates.
optsOptions-See Options API below.

Options API

ValueTypeDefaultDescription
shadowRootInitShadowRootInit{ mode: 'open' }the params for attachShadow
styleContentstringInline style string in shadow dom
styleSheetsstring[]Inline static css files in shadow dom, the base url is your root path.
1.2.0

1 year ago

1.1.4

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.1.1

2 years ago

1.0.0

2 years ago