Add a piece

Puzzle Pieces is a library of ready-made components: dialogs, sheets, toasts, menus, tables, charts and about a hundred more. They are not an npm package you import. puzzle add piece copies a piece's source into your app, with everything it depends on, and from then on it is your code.

Pieces are styled with Tailwind. Music's puzzle.config.js still declares the pipeline puzzle init set up, so puzzle add tailwind has nothing to do:

$ puzzle add tailwind
✓ puzzle.config.js already declares the Tailwind pipeline — nothing to do.

The app in this page has styled itself with plain app.css so far, so this lesson's files also bring in the Tailwind entry and the Tailwind packages in package.json. Now the dialog:

$ puzzle add piece dialog
puzzle add piece · npm:@magic-spells/puzzle-pieces@0.9.0
  ✓ dialog · 1 file
  ✓ theme/pieces.css · 1 file

  Next steps
    $ npm install @magic-spells/dialog-panel@^2.0.3
    → add `@import './pieces.css';` to app/styles/styles.css (after `@import "tailwindcss";`)

Those steps are done in this lesson's files. Open the tree to see what arrived: components/ui/Dialog.pzl, the design tokens in styles/pieces.css, and pieces.lock, the hash of every copied file. styles/styles.css is the new Tailwind entry: it imports the tokens, the stylesheet of the dialog-panel web component the Dialog wraps, and Music's own app.css.

The Dialog is driven by props. open opens it, title and description fill its header, showClose adds a close button, and it reports through @hide when it closes itself (Escape, the backdrop, the button), so the parent can set open back to false.

Your turn

The album covers now call @open={ show(album) }. Make them open a quick look:

  1. In Shop.pzl, import Dialog from '@/components/ui/Dialog.pzl'.
  2. Add the handlers. show remembers the album and opens; close only closes, so the content stays put while the dialog animates out:
show: (album) => {
  this.setData({ peekId: album.id, peeking: true });
  this.refresh();
},
close: () => {
  this.setData('peeking', false);
  this.refresh();
},
  1. In data(), return peekId: this.getData().peekId ?? '', peek (that album, found with findOne) and peeking: !!this.getData().peeking.
  2. Add the dialog at the end of the template:
<Dialog open={ peeking } title={ peek.title } description={ peek.artist } showClose={ true } @hide={ close }>
  {#if peek}
    <p class="notes">{ raw(peek.notes) }</p>
    <p>{ currency(peek.price) } · { date(peek.released, 'long') }</p>
  {/if}
  <a slot="footer" class="back" href={ link('/albums/' + peekId) }>Open the album page</a>
</Dialog>

Click a cover.

The preview's styles are built ahead of time from this lesson's solution, so a Tailwind class you invent here may have no CSS behind it. In your own app, puzzle dev rebuilds them as you type.

Chapters