Puzzle Playground

Puzzle —
<puzzle-view>
  <main>
    <p class="overline">A tiny browser spell</p>
    <div class="sparkle" aria-hidden="true">✦</div>
    <h1>{ title }</h1>
    <p>{ message }</p>
    <button type="button" @click={ castSpell }>
      Cast spell <span aria-hidden="true">→</span>
    </button>
    {#if count > 0}
      <small>Cast { pluralize(count, 'time') }</small>
    {:else}
      <small>Not cast yet</small>
    {/if}
  </main>
</puzzle-view>

<script>
import { PuzzleView } from '@magic-spells/puzzle';

const SPELLS = [
  ['Hello, Puzzle!', 'Your component compiled entirely in this tab.'],
  ['A little brighter', 'State, events, styles — the whole component is live.'],
  ['One more spark', 'Edit any line and the preview will rebuild itself.'],
];

export default class SparkCounter extends PuzzleView {
  created() {
    this.setData({ count: 0 });
  }

  data(params, props) {
    const count = this.getData().count ?? 0;
    const [title, message] = SPELLS[count % SPELLS.length];
    return { count, title, message };
  }

  events = {
    castSpell: () => {
      this.setData('count', this.getData().count + 1);
      this.refresh();
    },
  };
}
</script>

<style scoped>
  /* Scoped CSS is a native @scope rooted at the component's root element:
     :scope is that root, plain selectors reach its descendants. */
  :scope {
    min-height: 100%;
    display: grid;
    place-content: center;
    justify-items: center;
    gap: 1rem;
    padding: 3rem 1.5rem;
    color: #f4f4f5;
    text-align: center;
    background:
      radial-gradient(circle at 50% 20%, #3b2f70 0, transparent 38%),
      linear-gradient(145deg, #09090b, #15111f);
  }

  .overline {
    margin: 0;
    color: #a89cff;
    font: 600 0.7rem/1 ui-sans-serif, system-ui, sans-serif;
    letter-spacing: 0.22em;
    text-transform: uppercase;
  }

  .sparkle {
    color: #f2cc60;
    font-size: 2.5rem;
    filter: drop-shadow(0 0 1rem #f2cc6077);
  }

  h1 {
    margin: 0;
    font: 500 clamp(2.5rem, 9vw, 4.5rem)/0.95 Georgia, serif;
  }

  p {
    max-width: 32rem;
    margin: 0;
    color: #b4b4bb;
    font: 400 1rem/1.7 ui-sans-serif, system-ui, sans-serif;
  }

  button {
    border: 1px solid #6658c7;
    border-radius: 999px;
    padding: 0.8rem 1.25rem;
    color: white;
    background: #5c4fbd;
    font: 600 0.85rem/1 ui-sans-serif, system-ui, sans-serif;
    cursor: pointer;
  }

  button:hover { background: #7063d4; }
  button:focus-visible { outline: 3px solid #a89cff; outline-offset: 3px; }

  small {
    color: #77717f;
    font: 500 0.7rem/1 ui-monospace, monospace;
    letter-spacing: 0.08em;
    text-transform: uppercase;
  }
</style>
PreviewWaiting