Skip to content

Production-ready animations, transitions, backgrounds, and scenes for Remotion

5.8 KB
Raw Blame
{
  "type": "remotion-element",
  "version": 1,
  "element": {
    "assets": [],
    "dependencies": [],
    "dimensions": {
      "width": 220,
      "height": 60
    },
    "displayName": "Rolodex Flip",
    "initialProps": null,
    "slug": "rolodex-flip",
    "sourceCode": "import { Easing, Interactive, Sequence, interpolate, useCurrentFrame, type InteractiveBaseProps, type InteractiveTransformProps, type InteractivitySchema, type SequenceControls } from \"remotion\";\nimport React, { forwardRef, useImperativeHandle, useRef, type ComponentProps } from \"react\";\n\nconst clampOpts = {\n  extrapolateLeft: \"clamp\" as const,\n  extrapolateRight: \"clamp\" as const,\n};\n\ninterface RolodexFlipProps {\n  items: string[];\n  from?: number;\n  interval?: number;\n  flipDuration?: number;\n  className?: string;\n  style?: React.CSSProperties;\n}\n\nfunction RolodexFlipBase({\n  items,\n  from = 0,\n  interval = 20,\n  flipDuration = 10,\n  className,\n  style,\n}: RolodexFlipProps) {\n  const frame = useCurrentFrame();\n  const sizer = items.reduce((a, b) => (b.length > a.length ? b : a), \"\");\n  return (\n    <span\n      className={className}\n      style={{\n        position: \"relative\",\n        display: \"inline-block\",\n        perspective: 420,\n        verticalAlign: \"bottom\",\n        ...style,\n      }}\n    >\n      <span style={{ visibility: \"hidden\" }}>{sizer}</span>\n      {items.map((item, i) => {\n        const inStart = from + (i - 1) * interval + flipDuration / 2;\n        const outStart = from + i * interval;\n\n        let rotate = 0;\n        let y = 0;\n        let opacity = 1;\n\n        if (i > 0) {\n          const pIn = interpolate(\n            frame,\n            [inStart, inStart + flipDuration],\n            [0, 1],\n            { ...clampOpts, easing: Easing.out(Easing.cubic) },\n          );\n          rotate = (1 - pIn) * -85;\n          y = (1 - pIn) * 20;\n          opacity = pIn;\n        }\n        if (i < items.length - 1) {\n          const pOut = interpolate(\n            frame,\n            [outStart, outStart + flipDuration],\n            [0, 1],\n            { ...clampOpts, easing: Easing.in(Easing.cubic) },\n          );\n          rotate += pOut * 85;\n          y -= pOut * 20;\n          opacity *= 1 - pOut;\n        }\n        if (opacity <= 0.001) return null;\n\n        return (\n          <span\n            key={`${i}-${item}`}\n            style={{\n              position: \"absolute\",\n              left: 0,\n              top: 0,\n              whiteSpace: \"nowrap\",\n              opacity,\n              transform: `translateY(${y}px) rotateX(${rotate}deg)`,\n              transformOrigin: \"50% 50%\",\n              backfaceVisibility: \"hidden\",\n            }}\n          >\n            {item}\n          </span>\n        );\n      })}\n    </span>\n  );\n}\n\nconst elementSchema = {\n  ...Interactive.baseSchema,\n  interval: {\n    type: \"number\",\n    min: 10,\n    max: 40,\n    step: 2,\n    default: 20,\n    description: \"Interval\",\n    hiddenFromList: false,\n  },\n  flipDuration: {\n    type: \"number\",\n    min: 4,\n    max: 20,\n    step: 1,\n    default: 10,\n    description: \"Flip duration\",\n    hiddenFromList: false,\n  },\n  speed: {\n    type: \"number\",\n    default: 1,\n    min: 0.25,\n    max: 4,\n    step: 0.25,\n    description: \"Speed\",\n    hiddenFromList: false,\n  },\n  width: {\n    type: \"number\",\n    default: 220,\n    min: 10,\n    step: 10,\n    description: \"Width\",\n  },\n  height: {\n    type: \"number\",\n    default: 60,\n    min: 10,\n    step: 10,\n    description: \"Height\",\n  },\n  ...Interactive.transformSchema,\n} as const satisfies InteractivitySchema;\n\nconst ELEMENT_PROP_KEYS = new Set([\"interval\",\"flipDuration\",\"speed\"]);\n\nconst ELEMENT_PROP_DEFAULTS: Record<string, unknown> = {\n  interval: 20,\n  flipDuration: 10,\n  speed: 1,\n};\n\ntype RolodexFlipElementProps = InteractiveBaseProps &\n  InteractiveTransformProps & { readonly width?: number; readonly height?: number } &\n  ComponentProps<typeof RolodexFlipBase>;\n\nconst RolodexFlipInner = forwardRef<\n  HTMLDivElement,\n  RolodexFlipElementProps & { readonly controls: SequenceControls | undefined }\n>(({ controls, name, style, width = 220, height = 60, ...rest }, ref) => {\n  const outlineRef = useRef<HTMLDivElement>(null);\n  useImperativeHandle(ref, () => outlineRef.current as HTMLDivElement, []);\n  const componentProps: Record<string, unknown> = { ...ELEMENT_PROP_DEFAULTS };\n  const sequenceProps: Record<string, unknown> = {};\n  for (const [key, value] of Object.entries(rest)) {\n    if (ELEMENT_PROP_KEYS.has(key)) {\n      if (value !== undefined) componentProps[key] = value;\n    } else {\n      sequenceProps[key] = value;\n    }\n  }\n  return (\n    <Sequence\n      layout=\"none\"\n      {...sequenceProps}\n      controls={controls}\n      name={name ?? \"<RolodexFlip>\"}\n      outlineRef={outlineRef}\n    >\n      <div\n        style={{\n          position: \"absolute\",\n          inset: 0,\n          display: \"flex\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n        }}\n      >\n        <div\n          ref={outlineRef}\n          style={{\n            position: \"relative\",\n            width,\n            height,\n            ...style,\n          }}\n        >\n          <RolodexFlipBase\n            {...(componentProps as unknown as ComponentProps<typeof RolodexFlipBase>)}\n          />\n        </div>\n      </div>\n    </Sequence>\n  );\n});\n\nexport const RolodexFlip = Interactive.withSchema({\n  Component: RolodexFlipInner,\n  componentName: \"<RolodexFlip>\",\n  componentIdentity: null,\n  schema: elementSchema,\n  supportsEffects: false,\n});\n",
    "durationInFrames": 110,
    "installationMode": "component-owned-sequence"
  },
  "durationInFrames": 110
}