Skip to content

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

5.5 KB
Raw Blame
{
  "type": "remotion-element",
  "version": 1,
  "element": {
    "assets": [],
    "dependencies": [],
    "dimensions": {
      "width": 220,
      "height": 70
    },
    "displayName": "Value Swap",
    "initialProps": null,
    "slug": "value-swap",
    "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 ValueSwapProps {\n  values: string[];\n  at: number | number[];\n  duration?: number;\n  distance?: number;\n  direction?: \"up\" | \"down\";\n  className?: string;\n  style?: React.CSSProperties;\n}\n\nfunction ValueSwapBase({\n  values,\n  at,\n  duration = 10,\n  distance = 12,\n  direction = \"up\",\n  className,\n  style,\n}: ValueSwapProps) {\n  const frame = useCurrentFrame();\n  const ats = Array.isArray(at) ? at : [at];\n  const d = direction === \"down\" ? -distance : distance;\n  const count = Math.min(values.length, ats.length + 1);\n  const rendered = values.slice(0, count);\n  const sizer = rendered.reduce((a, b) => (b.length > a.length ? b : a), \"\");\n  const swapP = (start: number) =>\n    interpolate(frame, [start, start + duration], [0, 1], {\n      ...clampOpts,\n      easing: Easing.inOut(Easing.cubic),\n    });\n  return (\n    <span\n      className={className}\n      style={{\n        position: \"relative\",\n        display: \"inline-block\",\n        ...style,\n      }}\n    >\n      <span style={{ visibility: \"hidden\" }}>{sizer}</span>\n      {rendered.map((value, i) => {\n        const pIn = i > 0 ? swapP(ats[i - 1]) : 1;\n        const pOut = i < count - 1 ? swapP(ats[i]) : 0;\n        const opacity = pIn * (1 - pOut);\n        if (opacity <= 0.001) return null;\n        const y = (1 - pIn) * d + pOut * -d;\n        return (\n          <span\n            key={`${i}-${value}`}\n            style={{\n              position: \"absolute\",\n              left: 0,\n              top: 0,\n              whiteSpace: \"nowrap\",\n              opacity,\n              translate: `0 ${y}px`,\n            }}\n          >\n            {value}\n          </span>\n        );\n      })}\n    </span>\n  );\n}\n\nconst elementSchema = {\n  ...Interactive.baseSchema,\n  duration: {\n    type: \"number\",\n    min: 4,\n    max: 24,\n    step: 1,\n    default: 10,\n    description: \"Duration\",\n    hiddenFromList: false,\n  },\n  distance: {\n    type: \"number\",\n    min: 4,\n    max: 32,\n    step: 2,\n    default: 12,\n    description: \"Distance\",\n    hiddenFromList: false,\n  },\n  direction: {\n    type: \"enum\",\n    default: \"up\",\n    variants: {\n      up: {},\n      down: {},\n    },\n    description: \"Direction\",\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: 70,\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([\"duration\",\"distance\",\"direction\",\"speed\"]);\n\nconst ELEMENT_PROP_DEFAULTS: Record<string, unknown> = {\n  duration: 10,\n  distance: 12,\n  direction: \"up\",\n  speed: 1,\n};\n\ntype ValueSwapElementProps = InteractiveBaseProps &\n  InteractiveTransformProps & { readonly width?: number; readonly height?: number } &\n  ComponentProps<typeof ValueSwapBase>;\n\nconst ValueSwapInner = forwardRef<\n  HTMLDivElement,\n  ValueSwapElementProps & { readonly controls: SequenceControls | undefined }\n>(({ controls, name, style, width = 220, height = 70, ...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 ?? \"<ValueSwap>\"}\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          <ValueSwapBase\n            {...(componentProps as unknown as ComponentProps<typeof ValueSwapBase>)}\n          />\n        </div>\n      </div>\n    </Sequence>\n  );\n});\n\nexport const ValueSwap = Interactive.withSchema({\n  Component: ValueSwapInner,\n  componentName: \"<ValueSwap>\",\n  componentIdentity: null,\n  schema: elementSchema,\n  supportsEffects: false,\n});\n",
    "durationInFrames": 100,
    "installationMode": "component-owned-sequence"
  },
  "durationInFrames": 100
}