0.0.16 • Published 1 year ago

@ueberbit/custom-elements v0.0.16

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

Custom Element Api

The custom elements are build without any external dependencies. A custom jsx renderer based on createElement is used.

Custom Elements may use CE as a base Class:

import { CE } from '@ueberbit/utilities'

export class MyElement extends CE {
  static styles = `
    :host() {
      background: red;
    }
  `

  constructor() {
    super()
  }
  render() {
    return <div>Hi</div>
  }
}
window.customElements.define('my-element', MyElement)

declare global {
  interface HTMLElementTagNameMap {
    'my-element': MyElement
  }
}

Make sure to call super.connectedCallback() when overriding connectedCallback(), since render() is called inside it.

Styling

Styles from the static styles prop are applied to the custom Element. If supported adopted Stylesheets are used. Otherwise style tags are used. For elements without shadow root the styles are adopted to the document itself.

...
static styles = `
  :host() {
    background: red;
  }
`
...

For dynamic class binding the utility classMap can be used:

import { classMap } from '@ueberbit/utilities'

...

render() {
  return (
    <div className={classMap({
      'my-cool-class': true
    })}></div>
  )
}

...

Renderroot

Shadowroot is the default for renderroot. It is possible to override the renderroot with:

import { CE } from '@ueberbit/utilities'

export class LightDom extends CE {
  constructor() {
    super()
  }

  render() {
    return <div>Hello from Light Dom</div>
  }

  createRenderRoot() {
    return this
  }
}

window.customElements.define('light-dom', LightDom)

declare global {
  interface HTMLElementTagNameMap {
    'light-dom': LightDom
  }
}

Attribute Reflection

Properties can be reflected as attributes with reflectProp function.

...

open: boolean

constructor() {
  super()
  reflectProp(this, 'open')
}

...
0.0.15

1 year ago

0.0.16

1 year ago

0.0.14

2 years ago

0.0.10

2 years ago

0.0.11

2 years ago

0.0.12

2 years ago

0.0.13

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago