From d75b9a1e150ab211c1052fb49bede9bd16320aca Mon Sep 17 00:00:00 2001
From: Armand Philippot <git@armandphilippot.com>
Date: Sat, 7 Oct 2023 18:44:14 +0200
Subject: 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.
---
 src/components/atoms/flip/flip.tsx | 50 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
 create mode 100644 src/components/atoms/flip/flip.tsx

(limited to 'src/components/atoms/flip/flip.tsx')

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);
-- 
cgit v1.2.3

