Skip to content

πŸŽ₯ Make videos programmatically with React

Refine design system

JonnyBurger2bc8b758f4db
diff --git a/packages/design/src/Button.tsx b/packages/design/src/Button.tsx
index 24cbca04df..d829851f5b 100644
--- a/packages/design/src/Button.tsx
+++ b/packages/design/src/Button.tsx
@@ -2,12 +2,14 @@ import React, {useCallback, useRef, useState} from 'react';
import {cn} from './helpers/cn';
import {useHoverTransforms} from './helpers/hover-transforms';
import {Outer} from './helpers/Outer';
+import {Spinner} from './Spinner';
export const Button: React.FC<
React.ButtonHTMLAttributes<HTMLButtonElement> & {
readonly depth?: number;
+ readonly loading?: boolean;
}
-> = ({children, className, disabled, depth, ...buttonProps}) => {
+> = ({children, className, disabled, depth, loading, ...buttonProps}) => {
const [dimensions, setDimensions] = useState<{
width: number;
height: number;
@@ -15,7 +17,10 @@ export const Button: React.FC<
} | null>(null);
const ref = useRef<HTMLDivElement>(null);
- const {isActive, progress} = useHoverTransforms(ref, Boolean(disabled));
+ const {isActive, progress} = useHoverTransforms(
+ ref,
+ Boolean(disabled || loading),
+ );
const onPointerEnter = useCallback(
(e: React.PointerEvent<HTMLDivElement>) => {
@@ -72,7 +77,7 @@ export const Button: React.FC<
const content = (
<button
type="button"
- disabled={disabled}
+ disabled={disabled || loading}
className={cn(
'text-text',
'flex',
@@ -95,11 +100,22 @@ export const Button: React.FC<
'disabled:cursor-default',
'disabled:border-gray-500',
'disabled:text-gray-500',
+ 'relative',
+ 'overflow-hidden',
className,
)}
{...buttonProps}
>
- {children}
+ <div className={cn(loading && 'invisible')}>{children}</div>
+ {loading ? (
+ <div
+ className={cn(
+ 'absolute w-full h-full flex inset-0 items-center justify-center text-inherit bg-inherit',
+ )}
+ >
+ <Spinner size={20} duration={1} />
+ </div>
+ ) : null}
</button>
);
diff --git a/packages/design/src/Spinner.tsx b/packages/design/src/Spinner.tsx
new file mode 100644
index 0000000000..b48ad1eb5e
--- /dev/null
+++ b/packages/design/src/Spinner.tsx
@@ -0,0 +1,68 @@
+import React, {useEffect, useMemo, useRef} from 'react';
+
+const viewBox = 100;
+const lines = 8;
+
+// Generated from https://github.com/remotion-dev/template-next/commit/9282c93f7c51ada31a42e18a94680fa09a14eee3
+const translated =
+ 'M 44 0 L 50 0 a 6 6 0 0 1 6 6 L 56 26 a 6 6 0 0 1 -6 6 L 50 32 a 6 6 0 0 1 -6 -6 L 44 6 a 6 6 0 0 1 6 -6 Z';
+
+export const Spinner: React.FC<{
+ readonly size: number;
+ readonly duration: number;
+}> = ({size, duration}) => {
+ const style = useMemo(() => {
+ return {
+ width: size,
+ height: size,
+ };
+ }, [size]);
+
+ const pathsRef = useRef<(SVGPathElement | null)[]>([]);
+
+ useEffect(() => {
+ const animate = () => {
+ const now = performance.now();
+ for (let index = 0; index < lines; index++) {
+ const path = pathsRef.current[index];
+ if (!path) continue;
+ // Calculate current opacity using the same animation as before
+ const progress =
+ ((now / 1000 - index * (duration / lines)) % duration) / duration;
+ // Animation: opacity 1 β†’ 0.15
+ const opacity = 1 - 0.85 * progress;
+ path.style.opacity = opacity.toString();
+ }
+
+ requestAnimationFrame(animate);
+ };
+
+ const id = requestAnimationFrame(animate);
+ return () => {
+ cancelAnimationFrame(id);
+ };
+ }, [duration]);
+
+ return (
+ <svg style={style} viewBox={`0 0 ${viewBox} ${viewBox}`}>
+ {new Array(lines).fill(true).map((_, index) => {
+ return (
+ <path
+ // eslint-disable-next-line react/no-array-index-key
+ key={index}
+ // @ts-expect-error
+ // eslint-disable-next-line no-return-assign
+ ref={(el) => (pathsRef.current[index] = el)}
+ style={{
+ rotate: `${(index * Math.PI * 2) / lines}rad`,
+ transformOrigin: 'center center',
+ opacity: 1,
+ }}
+ d={translated}
+ fill={'currentColor'}
+ />
+ );
+ })}
+ </svg>
+ );
+};
diff --git a/packages/promo-pages/src/components/design.tsx b/packages/promo-pages/src/components/design.tsx
index c8981611a3..a4e6791ae6 100644
--- a/packages/promo-pages/src/components/design.tsx
+++ b/packages/promo-pages/src/components/design.tsx
@@ -59,10 +59,24 @@ export const DesignPage: React.FC = () => {
<Button className="bg-brand text-white">Primary</Button>
<br />
<Explainer>Click to disable</Explainer>
- <Button onClick={onClick} disabled={!submitButtonActive}>
+ <Button onClick={onClick} loading={!submitButtonActive}>
Submit
</Button>
<br />
+ <Explainer>Click to disable (primary)</Explainer>
+ <Button
+ onClick={onClick}
+ className="bg-brand text-white"
+ loading={!submitButtonActive}
+ >
+ Submit
+ </Button>
+ <br />
+ <Explainer>Loading state</Explainer>
+ <Button onClick={onClick} loading>
+ Loading
+ </Button>
+ <br />
<Explainer>Rounded</Explainer>
<Button className="rounded-full">Rounded</Button>
<br />