Created
January 26, 2022 14:13
-
-
Save nexpr/dc335498cd2838c6ae5d6f49dc261288 to your computer and use it in GitHub Desktop.
lit-html + emotion example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8" /> | |
<script> | |
// for emotion | |
window.process = { env: {} } | |
</script> | |
<script type="module"> | |
import { html, render } from "https://unpkg.com/[email protected]/lit-html.js" | |
import { css, cx } from "https://unpkg.com/@emotion/[email protected]/dist/emotion-css.esm.js?module" | |
const values = { | |
users: [ | |
{ | |
name: "user1", | |
description: "description".repeat(5), | |
}, | |
{ | |
name: "user2", | |
description: "description".repeat(4), | |
}, | |
{ | |
name: "user3", | |
description: "description".repeat(7), | |
}, | |
], | |
direction: "row", | |
} | |
const userTemplate = (user) => html` | |
<section class=${css` | |
border: 1px solid silver; | |
&:hover { | |
background: #f0f0f0; | |
} | |
& h1 { | |
font-size: 18px; | |
} | |
& p { | |
font-size: 12px; | |
color: #666"; | |
} | |
`}> | |
<h1>${user.name}</h1> | |
<p>${user.description}</p> | |
</section> | |
` | |
const radioTemplate = (items, name, selected, onchange) => items.map(item => html` | |
<label> | |
<input | |
type="radio" | |
name=${name} | |
?checked=${selected === item} | |
@change=${() => onchange(item)} | |
> | |
${item} | |
</label> | |
`) | |
const template = () => html` | |
<div> | |
<div> | |
${radioTemplate( | |
["row", "column"], | |
"direction", | |
values.direction, | |
update(selected => values.direction = selected) | |
)} | |
</div> | |
<div class=${css` | |
display:flex; | |
flex-direction: ${values.direction}; | |
gap:10px; | |
align-items: flex-start; | |
`}> | |
${values.users.map(user => userTemplate(user))} | |
</div> | |
</div> | |
` | |
const update = (fn) => (...a) => { | |
fn && fn(...a) | |
render(template(), root) | |
} | |
update()() | |
</script> | |
<div id="root"></div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment