All Projects → CaptainCodeman → redux-connect-element

CaptainCodeman / redux-connect-element

Licence: other
Redux HTMLElement Connector

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to redux-connect-element

highcharts-webcomponent
Highcharts Web Component usable with any Framework
Stars: ✭ 21 (+31.25%)
Mutual labels:  webcomponents, lit-html, lit-element
create-evergreen-app
Get up and running with an evergreen web application development stack designed by, and for, today's modern web.
Stars: ✭ 16 (+0%)
Mutual labels:  webcomponents, lit-html, lit-element
rollup-plugin-lit-css
Moved to https://github.com/bennypowers/lit-css
Stars: ✭ 35 (+118.75%)
Mutual labels:  webcomponents, lit-html, lit-element
polymerx-cli
⚡ Unlock the power of Polymer 3, Web Components and modern web tools.
Stars: ✭ 30 (+87.5%)
Mutual labels:  webcomponents, lit-html, lit-element
bce.design
minimal magic, minimal tooling, essential dependencies, high productivity, no transpilations and no migrations. The Web Components starter ships with integrated lit-html, redux-toolkit and vaadin router components.
Stars: ✭ 67 (+318.75%)
Mutual labels:  webcomponents, customelements, lit-html
pattern-library
AXA CH UI component library. Please share, comment, create issues and work with us!
Stars: ✭ 103 (+543.75%)
Mutual labels:  webcomponents, lit-html, lit-element
Wired Elements
Collection of custom elements that appear hand drawn. Great for wireframes or a fun look.
Stars: ✭ 8,848 (+55200%)
Mutual labels:  webcomponents, lit-html, lit-element
lit-components
Moved to https://github.com/vaadin/component-mixins
Stars: ✭ 59 (+268.75%)
Mutual labels:  webcomponents, lit-html, lit-element
fuco
Functional Component like React, but for Web Components.
Stars: ✭ 71 (+343.75%)
Mutual labels:  webcomponents, lit-html
wheat-ui
Web Components 组件库;拍平框架差异
Stars: ✭ 17 (+6.25%)
Mutual labels:  webcomponents, lit-html
pharos
JSTOR's design system
Stars: ✭ 48 (+200%)
Mutual labels:  lit-html, lit-element
identicon-avatar
👾 GitHub style identicon avatar
Stars: ✭ 15 (-6.25%)
Mutual labels:  webcomponents, customelements
pwa-lit-template
A template for building Progressive Web Applications using Lit and Vaadin Router.
Stars: ✭ 159 (+893.75%)
Mutual labels:  lit-html, lit-element
hello-web-components
A simple starter <hello-world /> web component written in typescript, using lit-html and lit-element. Unit tested with jest and e2e tested with puppeteer and jest-puppeteer.
Stars: ✭ 15 (-6.25%)
Mutual labels:  lit-html, lit-element
api-viewer-element
API documentation and live playground for Web Components. Based on Custom Elements Manifest format
Stars: ✭ 222 (+1287.5%)
Mutual labels:  webcomponents, lit-element
MoleculeJS
A library for creating fast and reactive Custom Elements
Stars: ✭ 39 (+143.75%)
Mutual labels:  webcomponents, lit-html
smart-webcomponents-community
Material & Bootstrap Web Components built with Smart
Stars: ✭ 30 (+87.5%)
Mutual labels:  webcomponents, customelements
smart-custom-element
Smart a lightweight web component library that provides capabilities for web components, such as data binding, using es6 native class inheritance. This library is focused for providing the developer the ability to write robust and native web components without the need of dependencies and an overhead of a framework.
Stars: ✭ 17 (+6.25%)
Mutual labels:  webcomponents, customelements
lego
🚀 Web-components made lightweight & Future-Proof.
Stars: ✭ 69 (+331.25%)
Mutual labels:  webcomponents, customelements
chartjs-web-components
the web components for chartjs
Stars: ✭ 50 (+212.5%)
Mutual labels:  webcomponents, lit-element

redux-connect-element

Connect Redux to vanilla HTMLElement (or LitElement) instances, based on this gist by Kevin Schaaf.

Typescript friendly and Tiny: 371 bytes (minified and gzipped)

Installation

npm install --save @captaincodeman/redux-connect-element

Usage

Your WebComponents can be kept 'pure' with no reference to Redux which helps to make them easily testable and reusable. They should accept properties to set their state and raise events to communicate their internal state changes.

A great library for writing lightweight custom elements is lit-element. Here's a very simple example:

import { LitElement, property, html } from 'lit-element'

export class MyElement extends LitElement {
  static get is() { return 'my-element' }

  @property({ type: String })
  public name: string = 'unknown'

  onChange(e: Event) {
    this.dispatchEvent(
      new CustomEvent('name-changed', { 
        bubbles: true,
        composed: trye,
        detail: e.target.value,
      })
    )
  }

  render() {
    return html`
      <p>Hello ${this.name}</p>
      <input type="text" .value=${this.name} @input=${this.onChange}>
    `
  }
}

This is the class you would import into tests - you can feed it whatever data you want with no need to setup external dependencies (such as Redux).

The connection to Redux can now be defined separately by subclassing the element and providing mapping functions. These map the Redux State to the element properties (mapState) and the events to Redux Actions (mapEvents).

The mapState method can map properties directly or you can make use of the Reselect library to memoize more complex projections.

import { connect } from '@captaincodeman/redux-connect-element'
import { store, State } from './store'
import { MyElement } from './my-element'

export class MyConnectedElement extends connect(store, MyElement) {
  // mapState provides the mapping of state to element properties
  // this can be direct or via reselect memoized functions
  mapState(state: State) {
    return {
      name: state.name,   
      // or using a reselecy selector:
      // name: NameSelector(state),
    }
  })

  // mapEvents provides the mapping of DOM events to redux actions
  // this can again be direct as shown below or using action creators
  mapEvents() {
    return {
      'name-changed': (e: NameChangedEvent) => ({
        type: 'CHANGE_NAME', 
        payload: { name: e.detail.name }
      })
      // or, using an action creator:
      // 'name-changed': (e: NameChangedEvent) => changeNameAction(e.detail.name)
    }
  }
}

Registering this element will make it 'connected' with it's properties kept in-sync with the Redux store and automatically re-rendered when they change. Mapped events are automatically dispatched to the store to mutate the state within Redux.

import { MyElementConnected } from './my-element-connected'

customElements.define(MyElement.is, MyElementConnected)

Of course if you prefer, you can include the connect mixin with the mapping functions directly in the element - having the split is entirely optional and down to personal style and application architecture.

I prefer to have a separate project for an apps elements which are pure UI components that have state set by properties and communicate with events. The app then consumes these building-block elements and uses connected views to connect the UI to the Redux state store.

Upgrading

If upgrading from v1, note that the mapping functions have been renamed and simplified.

State Mapping

Instead of:

_mapStateToProps = (state: State) => ({
  name: NameSelector(state)
})

Use:

mapState(state: State) {
  return {
    name: NameSelector(state),
  }
})

or

mapState = (state: State) => ({
  name: NameSelector(state),
})

Event Mapping

Instead of:

_mapEventsToActions = () => ({
  'name-changed': (e: NameChangedEvent) => changeNameAction(e.detail.name)
})

Or

_mapDispatchToEvents = (dispatch: Dispatch) => ({
  'name-changed': (e: NameChangedEvent) => dispatch(changeNameAction(e.detail.name))
})

Use:

mapEvents() {
  return {
    'name-changed': (e: NameChangedEvent) => changeNameAction(e.detail.name)
  }
}

Or

mapEvents = () => ({
  'name-changed': (e: NameChangedEvent) => changeNameAction(e.detail.name)
})
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].