Skip to content

Instantly share code, notes, and snippets.

@reggi
Last active December 12, 2023 21:15
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 reggi/a4bc79ec03b9cdb1ee9fd6c61ee8e081 to your computer and use it in GitHub Desktop.
Save reggi/a4bc79ec03b9cdb1ee9fd6c61ee8e081 to your computer and use it in GitHub Desktop.
import { WebComponent, html } from 'web-component-base'
class HFrag extends WebComponent {
static props = {
as: 'h1',
hover: false
}
text: string
constructor () {
super()
this.text = this.textContent
}
get template() {
const id = this.text
.match(/[A-Z]?[a-z]+|[0-9]+/g)
.join('-')
.toLowerCase()
const addWidth = this.props.hover ? 'width:40px;' : ''
return html`
<${this.props.as}>
<a
style="display:flex"
href="#${id}"
onMouseOver=${() => this.props.hover = true }
onMouseOut=${() => this.props.hover = false }>
<span style="overflow:hidden;width:0;transition: width 0.3s;${addWidth}">🔗</span>
<span>${this.text}</span>
</a>
</${this.props.as}>
`;
}
}
customElements.define('h-frag', HFrag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment