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.

3.6 KB
Raw Blame
import type { MouseEvent } from 'react';

import { Box } from '@chakra-ui/react';
import { IconButton } from '@platform/ui/Button';
import { Tooltip } from '@platform/ui/Tooltip';
import { ChevronLeftIcon, ChevronRightIcon } from 'lucide-react';
import { useCallback, useState } from 'react';
import { useTranslation } from 'react-i18next';

import type { PreviewSwipeNavigation } from './usePreviewSwipe';

/** Marks the arrows so the stage can reveal them on hover or focus within it. */
export const PREVIEW_NAV_ATTRIBUTE = 'data-preview-nav';

/** Arrows show on hover or focus within; hidden ones take no pointer events, so edge clicks reach the image. */
export const PREVIEW_NAV_STAGE_CSS = {
  [`& [${PREVIEW_NAV_ATTRIBUTE}]`]: {
    opacity: 0,
    pointerEvents: 'none',
    transitionDuration: '120ms',
    transitionProperty: 'opacity',
    transitionTimingFunction: 'ease-out',
    '@media (prefers-reduced-motion: reduce)': { transitionDuration: '0ms' },
  },
  [`&:hover [${PREVIEW_NAV_ATTRIBUTE}], &:focus-within [${PREVIEW_NAV_ATTRIBUTE}]`]: {
    opacity: 1,
    pointerEvents: 'auto',
  },
} as const;

/** Keeps the Preview's focus where it is, so the arrow keys still step after a click. */
const keepFocus = (event: MouseEvent) => event.preventDefault();

/** Pointer path to the arrow-key steps; out of the tab order since the keys already reach them. */
export const PreviewNavButtons = ({
  isVideo,
  navigation,
}: {
  isVideo: boolean;
  navigation: PreviewSwipeNavigation;
}) => {
  const { t } = useTranslation();
  const [isPending, setIsPending] = useState(false);
  const { neighbors, onNavigate } = navigation;
  const step = useCallback(
    (offset: -1 | 1) => {
      setIsPending(true);
      void onNavigate(offset).finally(() => setIsPending(false));
    },
    [onNavigate]
  );
  const stepPrevious = useCallback(() => step(-1), [step]);
  const stepNext = useCallback(() => step(1), [step]);

  return (
    <>
      {neighbors.previous ? (
        <NavButton
          isDisabled={isPending}
          isVideo={isVideo}
          keyShortcut="ArrowLeft"
          label={t('widgets.preview.commands.previousItem')}
          side="start"
          onStep={stepPrevious}
        />
      ) : null}
      {neighbors.next ? (
        <NavButton
          isDisabled={isPending}
          isVideo={isVideo}
          keyShortcut="ArrowRight"
          label={t('widgets.preview.commands.nextItem')}
          side="end"
          onStep={stepNext}
        />
      ) : null}
    </>
  );
};

const NavButton = ({
  isDisabled,
  isVideo,
  keyShortcut,
  label,
  side,
  onStep,
}: {
  isDisabled: boolean;
  isVideo: boolean;
  keyShortcut: string;
  label: string;
  side: 'end' | 'start';
  onStep: () => void;
}) => (
  <Box
    {...{ [PREVIEW_NAV_ATTRIBUTE]: side }}
    insetInlineEnd={side === 'end' ? '2' : undefined}
    insetInlineStart={side === 'start' ? '2' : undefined}
    position="absolute"
    // A video's control bar (about 4.5rem) overlays the bottom of its frame, which is centered in the stage; center
    // over the picture above the bar, so a short frame's scrubber never sits under an arrow.
    top={isVideo ? 'calc(50% - 2.25rem)' : '50%'}
    transform="translateY(-50%)"
    zIndex="1"
  >
    <Tooltip content={label}>
      <IconButton
        aria-keyshortcuts={keyShortcut}
        aria-label={label}
        bg="bg.panel/80"
        disabled={isDisabled}
        rounded="full"
        size="sm"
        tabIndex={-1}
        variant="outline"
        onClick={onStep}
        onMouseDown={keepFocus}
      >
        {side === 'start' ? <ChevronLeftIcon /> : <ChevronRightIcon />}
      </IconButton>
    </Tooltip>
  </Box>
);