For me React was the first library that made this possible
// create
class TodoItem extends HTMLElement {}
// register
customElements.define("todo-item", TodoItem);
// use
<todo-item></todo-item>
const View = styled.div`
color: #4d4d4d;
`;
// used like this
<View>
...
</View>
.htZyQy {
color: #4d4d4d;
}
...
:host{
background-color: #FFF:
}
:host(.dark) {
background-color: #000:
}
:root {
--todo-item-background-color: orange;
--todo-item-color: blue;
--todo-destroy-color: black;
}
<todo-item>
<slot/>
</todo-item>
"Just in the same way we don't ship vanilla JavaScript, no one will ship vanilla web components either"Jag Reehal
import { Component, State, Event, EventEmitter, h } from '@stencil/core';
@Component({
tag: 'todo-input'
})
export class TodoInput {
@Event({ eventName: 'input-submit' }) inputSubmit: EventEmitter;
@State() value: string;
keyUp = e => {
if (e.keyCode === 13) {
this.inputSubmit.emit(this.value);
this.value = '';
}
};
render() {
return (
<input
class="new-todo"
value={this.value}
type="text"
autoFocus
placeholder="What needs to be done?"
onInput={(ev: any) => (this.value = ev.target.value)}
onKeyUp={e => this.keyUp(e)}
/>
);
}
}
const TodoInput = () => {
const [textInput, onInputChange] = useState('');
const { onSubmitTodo } = React.useContext(TodoStoreContext);
const handleInputEnterPress = e => {
if (e.key === 'Enter') {
onSubmitTodo(textInput);
onInputChange('');
}
};
return (
<>
<input
onKeyPress={handleInputEnterPress}
className="new-todo"
placeholder="What needs to be done?"
autoFocus
value={textInput}
onChange={e => onInputChange(e.target.value)}
/>
</>
);
};
const TodoInput = () => {
const { onSubmitTodo } = React.useContext(TodoStoreContext);
const [customElementProps, ref] = useCustomElement({
'input-submit': onSubmitTodo
});
return <todo-input autofocus {...customElementProps} ref={ref} />;
};
<input class="new-todo"
autofocus
autocomplete="off"
placeholder="What needs to be done?"
v-model="newTodo"
@keyup.enter="addTodo" />
// tell Vue not to panic we know what we're doing
Vue.config.ignoredElements = ['todo-input'];
<todo-input @input-submit="addTodo"/>
Video player built with @stenciljs 🎸https://t.co/mTQGYNfsY8
— Stencil (@stenciljs) September 5, 2019