coffeekraken-s-lit-html-component v1.0.0
Coffeekraken s-lit-html-component
Table of content
- Demo
- Install
- Get Started
- State
- Directives
- Javascript API
- Sugar Web Components Documentation
- Browsers support
- Code linting
- Contribute
- Who are Coffeekraken?
- 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 | Firefox | Chrome | 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:
- ESLint with airbnb and prettier rules for javascript files
- 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:
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...
6 years ago