From ba793e043e4d8515b1a9ea490ee2c5f92b1fd6c2 Mon Sep 17 00:00:00 2001
From: Armand Philippot <git@armandphilippot.com>
Date: Wed, 27 Sep 2023 15:40:16 +0200
Subject: refactor(components): rewrite Section component

* Make it compliant with ESlint rules
* Remove mandatory heading, it now depends on the consumer
* Change defaults for hasBorder and variant
---
 src/components/atoms/layout/section/section.tsx | 50 +++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
 create mode 100644 src/components/atoms/layout/section/section.tsx

(limited to 'src/components/atoms/layout/section/section.tsx')

diff --git a/src/components/atoms/layout/section/section.tsx b/src/components/atoms/layout/section/section.tsx
new file mode 100644
index 0000000..63c658a
--- /dev/null
+++ b/src/components/atoms/layout/section/section.tsx
@@ -0,0 +1,50 @@
+import {
+  forwardRef,
+  type ForwardRefRenderFunction,
+  type HTMLAttributes,
+  type ReactNode,
+} from 'react';
+import styles from './section.module.scss';
+
+export type SectionVariant = 'dark' | 'light';
+
+export type SectionProps = Omit<HTMLAttributes<HTMLElement>, 'children'> & {
+  /**
+   * The section content.
+   */
+  children: ReactNode | ReactNode[];
+  /**
+   * Add a border at the bottom of the section.
+   *
+   * @default false
+   */
+  hasBorder?: boolean;
+  /**
+   * The section variant.
+   *
+   * @default 'light'
+   */
+  variant?: SectionVariant;
+};
+
+const SectionWithRef: ForwardRefRenderFunction<HTMLElement, SectionProps> = (
+  { children, className = '', hasBorder = false, variant = 'light', ...props },
+  ref
+) => {
+  const borderClass = hasBorder ? styles[`wrapper--borders`] : '';
+  const variantClass = styles[`wrapper--${variant}`];
+  const sectionClass = `${styles.wrapper} ${borderClass} ${variantClass} ${className}`;
+
+  return (
+    <section {...props} className={sectionClass} ref={ref}>
+      {children}
+    </section>
+  );
+};
+
+/**
+ * Section component
+ *
+ * Render a section element with a heading and a body.
+ */
+export const Section = forwardRef(SectionWithRef);
-- 
cgit v1.2.3

