aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/atoms/flip/flip.tsx
diff options
context:
space:
mode:
authorArmand Philippot <git@armandphilippot.com>2023-10-07 18:44:14 +0200
committerArmand Philippot <git@armandphilippot.com>2023-11-11 18:14:41 +0100
commitd75b9a1e150ab211c1052fb49bede9bd16320aca (patch)
treee5bb221d2b8dc83151697fe646e9194f921b5807 /src/components/atoms/flip/flip.tsx
parent12a03a9a72f7895d571dbaeeb245d92aa277a610 (diff)
feat(components): add a generic Flip component
The flipping animation is used at several places so it makes sense to use a single component to handle the animation. It will avoid styles duplication.
Diffstat (limited to 'src/components/atoms/flip/flip.tsx')
-rw-r--r--src/components/atoms/flip/flip.tsx50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/components/atoms/flip/flip.tsx b/src/components/atoms/flip/flip.tsx
new file mode 100644
index 0000000..df77e9a
--- /dev/null
+++ b/src/components/atoms/flip/flip.tsx
@@ -0,0 +1,50 @@
+import {
+ type ForwardRefRenderFunction,
+ type HTMLAttributes,
+ type ReactNode,
+ forwardRef,
+} from 'react';
+import styles from './flip.module.scss';
+
+export type FlipProps = Omit<HTMLAttributes<HTMLDivElement>, 'children'> & {
+ /**
+ * The front and back sides.
+ */
+ children: ReactNode;
+ /**
+ * The animation direction.
+ *
+ * @default 'horizontal'
+ */
+ direction?: 'horizontal' | 'vertical';
+ /**
+ * Should we show back side?
+ *
+ * It let you control dynamically which side to show. When set to `true` the
+ * hover/focus animation will be removed.
+ *
+ * @default undefined
+ */
+ showBack?: boolean;
+};
+
+const FlipWithRef: ForwardRefRenderFunction<HTMLDivElement, FlipProps> = (
+ { children, className = '', direction = 'horizontal', showBack, ...props },
+ ref
+) => {
+ const wrapperClass = [
+ styles.wrapper,
+ styles[`wrapper--${direction}`],
+ styles[showBack === undefined ? 'wrapper--dynamic' : 'wrapper--manual'],
+ styles[showBack ? 'wrapper--is-back' : 'wrapper--is-front'],
+ className,
+ ].join(' ');
+
+ return (
+ <div {...props} className={wrapperClass} ref={ref}>
+ {children}
+ </div>
+ );
+};
+
+export const Flip = forwardRef(FlipWithRef);