coffeekraken-s-template-component v1.0.5
Coffeekraken s-template-component
Table of content
- Install
- Get Started
- State
- Key
- Model
- JSX pragma
- Javascript API
- Sugar Web Components Documentation
- Browsers support
- Code linting
- Contribute
- Who are Coffeekraken?
- Licence
Install
npm install coffeekraken-s-template-component --save
Get Started
First, create a new webcomponent based on the STemplateComponent class like so:
import STemplateComponent from "coffeekraken-s-template-component"
export default class MyCoolWebComponent extends STemplateComponent {
/**
* Default props
* @definition SWebComponent.defaultProps
* @protected
*/
static get defaultProps() {
return {
aCoolProp: "Hello Props"
}
}
/**
* Default prstateops
* @definition STemplateComponent.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(
<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 STemplateComponent {
// 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"
}
}
Key
The key is an important concept for your templates to work as expected. When you have some list of elements, you'll need to add a key
property to each list item. This key
property need to be unique and consistent. This mean that you don't have to use the index
of your loop/map/etc...
Here's an example of correct usage:
<ul>
{todos.map(todo => (
<li key={todo.id}>{todo.title}</li>
))}
</ul>
Model
In order to simplify the form integration, a property called model
is available. A model is an object that is composed of these properties:
obj
: The object on which the model pointkey
: The key path on theobj
on which the model pointtrigger
: The event that trigger the model update. By default it'sinput
for text input andchange
for select, checkbox and radio
Here's an example of usage:
// with an input
<input type="text" name="my-cool-input" model={{obj:this,key:'state.myValue'}} />
// with some checkboxes (the state.checkbox has to be an array)
<input type="checkbox" name="my-checkbox" value="Hello" model={{obj:this,key:'state.checkbox'}} />
<input type="checkbox" name="my-checkbox" value="World" model={{obj:this,key:'state.checkbox'}} />
// with a select
<select name="my-select" model={{obj:this,key:'state.select'}}>
<option value="hello">Hello</option>
// etc...
</select>
JSX Pragma
In order to render the JSX with the good createElement
function call, you'll need to set the JSX pragma compiler option like so in your .babelrc
file:
{
"plugins": [
["@babel/plugin-transform-react-jsx", {
"pragma": "STemplateComponent.createElement"
}]
]
}
You can also specify the pragma in your component code like so:
/** @jsx STemplateComponent.createElement */
// your code here...
Browsers support
IE / Edge | Firefox | Chrome | Safari |
---|---|---|---|
*IE11+ | last 2 versions | last 2 versions | last 2 versions |
- This component use the
Proxy
feature that is not natively supported by IE11. You'll need to load a polyfill in order to make it work properly.
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:
- StandardJS 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...