Skip to content

Instantly share code, notes, and snippets.

@dy
Last active August 1, 2019 02:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dy/a556d817042c3d492455e93831fd3572 to your computer and use it in GitHub Desktop.
Save dy/a556d817042c3d492455e93831fd3572 to your computer and use it in GitHub Desktop.
Spect style
import $ from 'spect'
import { route } from 'wouter'
import { t } from 'ttag'
// main aspect
$('#app').aspect($app => {
let [ , { id } ] = route('user/:id')
$app.fx(() => $app
.attr({ loading: true })
.data({ user: await fetch('./user')})
.attr({ loading: false })
, id)
$app.html`<div.t.preloader>Hello, ${$app.data('user').name}!</div>`
})
// preloader aspect
$('.preloader').aspect($el => {
if ($el.attr('loading')) $el.html`${ $el.children } <canvas class="spinner" />`
})
// i18n aspect
$('.t').aspect($el => $el.html`${ t`${ $el.text() }` }`)
import $ from 'spect'
// main aspect
function app ($el) {
let [ match, { id } ] = $el.route('user/:id') // we need $el here to attach updating el effects
if (!match) return
$el.fx(async () => {
$el.loading = true
$el.user = await ky.get(`/api/user/${id}`)
$el.loading = false
}, id)
$el.fx(preloader, $el.loading)
$el.html`<div fx=${i18n}>Hello, ${ $el.user.name }!</div>`
})
// i18n aspect
function i18n ($el) {
useLocale($(document.documentElement).attr.lang)
html`${ t`${ $el.text }` }`
}
// preloader aspect
function preloader ($el) {
if ($el.loading) $el.html`${ el.childNodes } <canvas class="spinner" />`
})
$(el).fx(app)
import $, { html, state, fx, route, attr } from 'spect'
import { t } from 'ttag'
// main aspect
$('#app', app => {
let [ match, { id } ] = route('user/:id')
let { user } = state()
fx(async () => {
attr({ loading: true })
state({ user: await fetch('/user') })
attr({ loading: false })
}, [id])
html`<div.t.preloader>Hello, ${user.name}!</div>`
})
// preloader aspect
$('.preloader', el => {
let { loading = false } = attr()
if (loading) html`${el.childNodes} <canvas class="spinner" />`
})
// i18n aspect
$('.t', el => {
let lang = el.lang || document.documentElement.lang
html`${ t`${el.textContent}` }`
})
import { useAspect, useRoute, useAttr, useState, useHtml, useEffect } from 'spect'
import { t, useLocale } from 'ttag'
import ky from 'ky'
// main aspect
function app (el) {
useAspect(preloader)
let [ match, { id } ] = useRoute('user/:id')
let attr = useAttr({ loading: false })
let state = useState({ user: null })
let html = useHtml()
useEffect(async () => {
attr.loading = true
state.user = await ky.get(`/api/user/${id}`)
attr.loading = false
}, id)
html`<div use=${i18n}>Hello, ${ state.user.name }!</div>`
})
// i18n aspect
function i18n (el) {
let { lang } = useAttr(document.documentElement)
useLocale(lang)
let html = useHtml()
html`${ t`${ el.textContent }` }`
}
// preloader aspect
function preloader (el) {
let { loading } = useAttr()
let html = useHtml()
if (loading) html`${ el.childNodes } <canvas class="spinner" />`
})
// attach aspects to target
useAspect(document.querySelector('#app'), app)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment