Families

Some components only make sense together, like a shelf, its title and its row. A family groups them under one import, used with dotted names:

<Shelf>
  <Shelf.Title>Staff picks</Shelf.Title>
  <Shelf.Row>
    {#for album in picks}
      <AlbumCard album={ album } @add={ add(album) }/>
    {/for}
  </Shelf.Row>
</Shelf>

There is no registry and no compiler magic. A family is a folder of ordinary components plus a plain JavaScript barrel, index.js, that hangs the members on the root:

export { Shelf, Title, Row };
export default Object.assign(Shelf, { Title, Row });

So import Shelf from '@/components/Shelf' brings the whole family, and <Shelf.Title> is just Shelf.Title. Each member stays one component per file, composing through <Children/>. The CLI writes the folder for you:

$ puzzle generate component Shelf --family Title,Row
✓ created app/components/Shelf/
  app/components/Shelf/Shelf.pzl
  app/components/Shelf/Title.pzl
  app/components/Shelf/Row.pzl
  app/components/Shelf/index.js

This lesson's Shelf has become that family (open the files on the left), and Shop.pzl already imports it. The shelves have lost their titles and their grid: the family's Shelf has no title slot, and nothing wraps the cards in a row.

Your turn

Rewrite both shelves in Shop.pzl with the family's members: <Shelf.Title> in place of the slot="title" span, and a <Shelf.Row> around each loop.

Named slots or a family? Slots suit one component with a few fixed regions. A family suits parts the caller arranges, repeats or leaves out.

Chapters