Skip to content

Instantly share code, notes, and snippets.

@davismj
Created February 1, 2024 20:48
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 davismj/69edd8e4f3d5c89e9570d0a0d8f8b7bc to your computer and use it in GitHub Desktop.
Save davismj/69edd8e4f3d5c89e9570d0a0d8f8b7bc to your computer and use it in GitHub Desktop.
Number Pad web component as Astro component
<number-pad {...Astro.props}></number-pad>
<script>
import { FASTElement, css, customElement, html } from "@microsoft/fast-element";
import styles from './number-pad.scss?inline';
const template = html<NumberPad>`
<div class="number-pad" @click="${(x, c) => x.handleClick(c.event)}">
<div class="number-pad-row">
<span role="button">1</span>
<span role="button">2</span>
<span role="button">3</span>
</div>
<div class="number-pad-row">
<span role="button">4</span>
<span role="button">5</span>
<span role="button">6</span>
</div>
<div class="number-pad-row">
<span role="button">7</span>
<span role="button">8</span>
<span role="button">9</span>
</div>
<div class="number-pad-row">
<span role="button">.</span>
<span role="button">0</span>
<span role="button">&lt;</span>
</div>
</div>
`;
@customElement({
name: 'number-pad',
template,
styles
})
class NumberPad extends FASTElement {
handleClick({ target }: Event) {
const button = target as HTMLElement | null;
if (button?.matches('[role="button"]')) {
this.dispatchEvent(new InputEvent('input', {
data: button.innerText,
bubbles: true,
}));
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment