Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

11.5 KB
Raw Blame
---
// Showcase artwork in justified rows that keep each image's aspect ratio. A tile opens the artwork in a dialog with its
// full credit, and so does a link to the tile's anchor, `/showcase/#showcase-<entry id>`.
import ArrowLeft from '@lucide/astro/icons/arrow-left';
import ArrowRight from '@lucide/astro/icons/arrow-right';
import X from '@lucide/astro/icons/x';
import type { CollectionEntry } from 'astro:content';
import { getImage, Image } from 'astro:assets';

type Props = {
  entries: CollectionEntry<'showcase'>[];
};

const { entries } = Astro.props;
const tileId = (entry: CollectionEntry<'showcase'>) => `showcase-${entry.id}`;
const dateFormat = new Intl.DateTimeFormat('en', { dateStyle: 'long', timeZone: 'UTC' });
const maxWidth = 2400;
const lightboxWidths = (width: number) => [960, 1600, maxWidth].filter((candidate) => candidate < width).concat(Math.min(width, maxWidth));
// Without JavaScript, or on a modified click, a tile opens its largest size. Astro emits only the sizes it is asked for.
const fullImages = await Promise.all(
  entries.map(({ data: { image } }) => getImage({ src: image, width: Math.min(image.width, maxWidth) }))
);
---

<div class="showcase not-content">
  <ul class="grid" role="list">
    {
      entries.map((entry, index) => {
        const { image, title, artist, model } = entry.data;
        return (
          <li class="tile" id={tileId(entry)} style={`--ratio: ${image.width / image.height}`}>
            <a href={fullImages[index].src} data-showcase-index={index}>
              {/* The caption names the artwork; the lightbox gives its full credit. */}
              <Image
                src={image}
                alt=""
                widths={[320, 480, 720, 960].filter((width) => width < image.width).concat(image.width)}
                sizes={`(min-width: 50rem) ${Math.ceil((image.width / image.height) * 22)}rem, 100vw`}
              />
              <span class="caption">
                <span class="caption__title">{title}</span>
                <span class="caption__meta">
                  {artist}
                  {model && ` · ${model}`}
                </span>
              </span>
            </a>
          </li>
        );
      })
    }
  </ul>

  {entries.map(({ data: { image, title, artist, artistUrl, model, createdWith, date } }) => (
    <template data-showcase-credit>
      <figure class="artwork">
        <div class="artwork__image">
          <Image
            src={image}
            alt={title}
            widths={lightboxWidths(image.width)}
            sizes="min(90rem, 100vw)"
            loading="eager"
          />
        </div>
        <figcaption class="credit">
          <h2 id="showcase-lightbox-title">{title}</h2>
          <p class="credit__artist">
            by{' '}
            {artistUrl ? (
              <a href={artistUrl} target="_blank" rel="nofollow ugc noopener noreferrer">
                {artist}
              </a>
            ) : (
              artist
            )}
          </p>
          <dl>
            {model && (
              <div>
                <dt>Model</dt>
                <dd>{model}</dd>
              </div>
            )}
            {createdWith && (
              <div>
                <dt>Made with</dt>
                <dd>{createdWith}</dd>
              </div>
            )}
            <div>
              <dt>Added</dt>
              <dd>
                <time datetime={date.toISOString().slice(0, 10)}>{dateFormat.format(date)}</time>
              </dd>
            </div>
          </dl>
        </figcaption>
      </figure>
    </template>
  ))}

  <dialog class="lightbox" aria-labelledby="showcase-lightbox-title" data-showcase-lightbox>
    <div class="lightbox__frame">
      <div class="lightbox__toolbar">
        <p class="lightbox__counter" data-lightbox-counter />
        <div class="lightbox__buttons">
          {entries.length > 1 && (
            <>
              <button type="button" class="docs-button docs-button--ghost docs-button--icon docs-button--lg" data-lightbox-step="-1" aria-label="Previous artwork">
                <ArrowLeft aria-hidden="true" />
              </button>
              <button type="button" class="docs-button docs-button--ghost docs-button--icon docs-button--lg" data-lightbox-step="1" aria-label="Next artwork">
                <ArrowRight aria-hidden="true" />
              </button>
            </>
          )}
          <button type="button" class="docs-button docs-button--ghost docs-button--icon docs-button--lg" data-lightbox-close aria-label="Close" autofocus>
            <X aria-hidden="true" />
          </button>
        </div>
      </div>
      <div class="lightbox__content" data-lightbox-content />
    </div>
  </dialog>
</div>

<script>
  const dialog = document.querySelector<HTMLDialogElement>('[data-showcase-lightbox]');
  if (dialog) {
    const tiles = [...document.querySelectorAll<HTMLAnchorElement>('[data-showcase-index]')];
    const credits = [...document.querySelectorAll<HTMLTemplateElement>('template[data-showcase-credit]')];
    const content = dialog.querySelector<HTMLElement>('[data-lightbox-content]')!;
    const counter = dialog.querySelector<HTMLElement>('[data-lightbox-counter]')!;
    let current = 0;

    const show = (index: number) => {
      current = (index + tiles.length) % tiles.length;
      content.replaceChildren(credits[current].content.cloneNode(true));
      counter.textContent = `${current + 1} of ${tiles.length}`;
    };

    const open = (index: number) => {
      show(index);
      if (!dialog.open) dialog.showModal();
    };

    // A link to a tile's anchor (`#showcase-<entry id>`) opens its artwork. Nothing here adds history entries, so
    // Back and Forward leave the page as usual; a hash that names no artwork closes the dialog.
    const tileIndexFromHash = () => {
      const id = decodeURIComponent(location.hash.slice(1));
      return id ? tiles.findIndex((tile) => tile.parentElement?.id === id) : -1;
    };
    const openFromHash = () => {
      const index = tileIndexFromHash();
      if (index >= 0) open(index);
      else if (dialog.open) dialog.close();
    };

    tiles.forEach((tile, index) => {
      tile.addEventListener('click', (event) => {
        // Modified clicks still open the full-size image the link points at.
        if (event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
        event.preventDefault();
        open(index);
      });
    });

    dialog.querySelectorAll<HTMLButtonElement>('[data-lightbox-step]').forEach((button) => {
      button.addEventListener('click', () => show(current + Number(button.dataset.lightboxStep)));
    });
    dialog.querySelector('[data-lightbox-close]')?.addEventListener('click', () => dialog.close());

    dialog.addEventListener('keydown', (event) => {
      if (tiles.length < 2 || (event.key !== 'ArrowLeft' && event.key !== 'ArrowRight')) return;
      event.preventDefault();
      show(current + (event.key === 'ArrowLeft' ? -1 : 1));
    });
    // The frame fills the dialog, so a click on the dialog itself is a click on the backdrop.
    dialog.addEventListener('click', (event) => {
      if (event.target === dialog) dialog.close();
    });
    dialog.addEventListener('close', () => {
      content.replaceChildren();
      // Leave the URL clean, replacing the current entry rather than adding one.
      if (location.hash) history.replaceState(history.state, '', location.pathname + location.search);
      tiles[current].focus();
    });

    window.addEventListener('hashchange', openFromHash);
    openFromHash();
  }
</script>

<style>
  .showcase {
    --row-height: 11rem;
  }

  @media (min-width: 50rem) {
    .showcase {
      --row-height: 16rem;
    }
  }

  .grid {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin: 0;
    padding: 0;
    list-style: none;

    /* Takes the free space in the last row, so its tiles keep the row height instead of stretching. */
    &::after {
      content: '';
      flex-grow: 1000000;
    }
  }

  /* Growing and shrinking in proportion to the aspect ratio keeps every tile in a row at the same height. */
  .tile {
    flex: var(--ratio) 1 calc(var(--ratio) * var(--row-height));
    min-width: 0;
  }

  .tile a {
    position: relative;
    display: block;
    overflow: hidden;
    border-radius: var(--docs-radius-container);
    background-color: var(--docs-surface);
    color: var(--docs-text-strong);
    text-decoration: none;
  }

  .tile img {
    display: block;
    width: 100%;
    height: auto;
    aspect-ratio: var(--ratio);
  }

  .caption {
    position: absolute;
    inset: auto 0 0;
    display: flex;
    flex-direction: column;
    gap: 0.125rem;
    padding: 2rem 0.75rem 0.625rem;
    background: linear-gradient(transparent, color-mix(in oklab, var(--docs-bg) 92%, transparent));
    font-size: 0.8125rem;
    line-height: 1.35;
    opacity: 0;
    transition: opacity 150ms ease;
  }

  .tile a:hover .caption,
  .tile a:focus-visible .caption {
    opacity: 1;
  }

  /* Touch screens have no hover, so the caption always shows. */
  @media (hover: none) {
    .caption {
      opacity: 1;
    }
  }

  @media (prefers-reduced-motion: reduce) {
    .caption {
      transition: none;
    }
  }

  .caption__title {
    font-weight: 600;
  }

  .caption__meta {
    color: var(--docs-text-muted);
  }

  /* Lightbox */

  /* Starlight's reset removes every margin, which would pin the modal to the top-left corner. */
  .lightbox {
    position: fixed;
    inset: 0;
    width: min(96vw, 100rem);
    max-width: none;
    height: fit-content;
    max-height: 92dvh;
    margin: auto;
    padding: 0;
    border: 1px solid var(--docs-border);
    border-radius: var(--docs-radius-container);
    background-color: var(--docs-surface);
    color: var(--docs-text);
    box-shadow: var(--docs-shadow-popup);

    &::backdrop {
      background-color: color-mix(in oklab, var(--docs-bg) 85%, transparent);
    }
  }

  .lightbox__frame {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    padding: 0.75rem;
  }

  .lightbox__toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
  }

  .lightbox__counter {
    margin: 0;
    color: var(--docs-text-subtle);
    font-size: var(--sl-text-xs);
    font-variant-numeric: tabular-nums;
  }

  .lightbox__buttons {
    display: flex;
    gap: 0.25rem;
  }

  .artwork {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    margin: 0;
  }

  /* Leaves room in the dialog for the toolbar and the credit, so the whole artwork fits without scrolling. */
  .artwork__image img {
    display: block;
    width: auto;
    max-width: 100%;
    height: auto;
    max-height: calc(92dvh - 10rem);
    margin-inline: auto;
    object-fit: contain;
    border-radius: var(--docs-radius);
  }

  @media (max-width: 49.99rem) {
    .artwork__image img {
      max-height: calc(92dvh - 13rem);
    }
  }

  .credit {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: 0.25rem 1.5rem;
    padding: 0 0.25rem 0.25rem;

    h2 {
      margin: 0;
      color: var(--docs-text-strong);
      font-size: 1.125rem;
      font-weight: 600;
    }

    a {
      color: var(--docs-accent);

      &:hover {
        color: var(--docs-accent-text);
      }
    }
  }

  .credit__artist {
    margin: 0;
    color: var(--docs-text-muted);
  }

  .credit dl {
    display: flex;
    flex-wrap: wrap;
    gap: 0.25rem 1.25rem;
    margin: 0;
    font-size: var(--sl-text-sm);

    div {
      display: flex;
      gap: 0.375rem;
    }

    dt {
      color: var(--docs-text-subtle);
    }

    dd {
      margin: 0;
      color: var(--docs-text);
    }
  }
</style>