Skip to content

Instantly share code, notes, and snippets.

@shiftyp
Last active October 10, 2024 13:58
Show Gist options
  • Save shiftyp/4f8d99719c1a6ee0c723fd19a1f2d1f1 to your computer and use it in GitHub Desktop.
Save shiftyp/4f8d99719c1a6ee0c723fd19a1f2d1f1 to your computer and use it in GitHub Desktop.
const div = document.createElement('div')
const input = document.createElement('input')
input.setAttribute('type', 'text')
input.addEventListener('change', (e) => {
if (text === '') {
return updateDiv({ text, className: 'hidden'})
}
updateDiv(text, className: 'my-class')
});
const updateDiv = ({ text, className }) => {
div.innerText = text
if (className !== div.getAttribute('class')) {
div.setAttribute('class', className)
}
}
document.body.appendElement(div)
document.body.appendElement(input)
updateDiv({ text: 'I\'m some text', className: 'my-class' })
import { render, useSignal } from 'preact'
const Component = () => {
const text = useSignal('I\'m some text')
return (
<>
<div className={text.value ? 'my-class' : 'hidden'}>
{text.value}
</div>
<input onChange={(e) => text.value = e.target.value} type="text" />
</>
)
}
render(<Component />, document.body)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment