Have ideas to improve npm?Join in the discussion! »

    coffeekraken-s-lit-html-component

    1.0.0 • Public • Published

    Coffeekraken s-lit-html-component

    Base class that allows the creation of [lit-html](https://lit-html.polymer-project.org/) based webcomponent.

    View demo

    Table of content

    1. Demo
    2. Install
    3. Get Started
    4. State
    5. Directives
    6. Javascript API
    7. Sugar Web Components Documentation
    8. Browsers support
    9. Code linting
    10. Contribute
    11. Who are Coffeekraken?
    12. Licence

    Install

    npm install coffeekraken-s-lit-html-component --save
    

    Get Started

    First, create a new webcomponent based on the STemplateComponent class like so:

    import SLitHtmlComponent from "coffeekraken-s-template-component"
    import { html } from 'lit-html'
     
    export default class MyCoolWebComponent extends SLitHtmlComponent {
      /**
       * Default props
       * @definition    SWebComponent.defaultProps
       * @protected
       */
      static get defaultProps() {
        return {
          aCoolProp: "Hello Props"
        }
      }
     
      /**
       * Default prstateops
       * @definition    SWebComponent.defaultState
       * @protected
       */
      static get defaultState() {
        return {
          aCoolStateItem: "Hello State"
        }
      }
     
      /**
       * Render the component
       * @definition    SWebComponent.render
       */
      render() {
        // feed a JSX template into the super.render function
        super.render(html`
          <header>
            <h1>${this.props.aCoolProp}</h1>
            <h2>${this.state.aCoolStateItem}</h2>
          </header>
        `)
      }
    }
     
    // register the new component
    MyCoolWebComponent.define("my-cool-web-component")

    Then simply use it inside your html like so:

    <my-cool-web-component a-cool-prop="Hello World"></my-cool-web-component>

    State

    State is pretty much like props, with the difference that state is for storing internal state. In the other hand, props are used to store external component state.

    Here's how to use state:

    class MyCoolComponent extends SLitHtmlComponent {
      // define your state variables
      static get defaultState() {
        return {
          myCoolStateVariable: "Hello"
        }
      }
     
      componentMount() {
        // access your state variables
        console.log(this.state.myCoolStateVariable) // Hello
        // set your state variables
        this.setState({
          myCoolStateVariable: "World"
        })
        // or directly by seeting the variable
        this.state.myCoolStateVariable = "World"
      }
    }

    Directives

    As lit-html provide some directives, this component provide his own. Here's the list:

    fn

    The fn directive has to be used when you want to pass a function to a SWebComponent through his attributes. Here's how:

    import { html } from 'lit-html'
    import SLitHtmlComponent, {fn} from 'coffeekraken-s-lit-html-component'
    class MyCoolComponent extends SLitHtmlComponent {
      myCoolFunction() {
        console.log('hello world')
      }
      render() {
        super.render(html`
          <my-other-component pass-a-function=${fn(this.myCoolFunction.bind(this))}>/my-other-component>
        `)
      }
    }
    MyCoolComponent.define('my-cool-component')

    json

    The json directive is a shortcut for JSON.stringify. Nothing more, nothing less. It has to be used when you want to pass a plain object/array to a component through his attributes. Here's how:

    import { html } from 'lit-html'
    import SLitHtmlComponent, {json} from 'coffeekraken-s-lit-html-component'
    class MyCoolComponent extends SLitHtmlComponent {
      render() {
        const myPlainObject = {
          hello: 'World'
        }
        super.render(html`
          <my-other-component pass-an-object=${json(myPlainObject)}>/my-other-component>
        `)
      }
    }
    MyCoolComponent.define('my-cool-component')

    Browsers support

    IE / Edge
    IE / Edge
    Firefox
    Firefox
    Chrome
    Chrome
    Safari
    Safari
    IE11+ last 2 versions last 2 versions last 2 versions

    As browsers are automatically updated, we will keep as reference the last two versions of each but this component can work on older ones as well.

    The webcomponent API (custom elements, shadowDOM, etc...) is not supported in some older browsers like IE10, etc... In order to make them work, you will need to integrate the corresponding polyfill.

    Code linting

    This package uses some code linting rules. Here's the list:

    1. ESLint with airbnb and prettier rules for javascript files
    2. Stylelint with stylelint-config-standard for scss files

    Your commits will not been accepted if the code style is not respected!

    Contribute

    This is an open source project and will ever be! You are more that welcomed to contribute to his development and make it more awesome every day. To do so, you have several possibilities:

    1. Share the love ❤️
    2. Declare issues
    3. Fix issues
    4. Add features
    5. Build web component

    Who are Coffeekraken

    We try to be some cool guys that build some cool tools to make our (and yours hopefully) every day life better.

    More on who we are

    License

    The code is available under the MIT license. This mean that you can use, modify, or do whatever you want with it. This mean also that it is shipped to you for free, so don't be a hater and if you find some issues, etc... feel free to contribute instead of sharing your frustrations on social networks like an asshole...

    Install

    npm i coffeekraken-s-lit-html-component

    DownloadsWeekly Downloads

    0

    Version

    1.0.0

    License

    MIT

    Unpacked Size

    1.49 MB

    Total Files

    32

    Last publish

    Collaborators

    • avatar