From 852ebb915bda2179133e34c2b7fdf7d2f3a0229e Mon Sep 17 00:00:00 2001 From: Cicero Viana Date: Wed, 4 May 2022 21:53:25 -0300 Subject: [PATCH] chore(mfe-sidebar): refact sidebar component. --- packages/eslint-config/src/eslintrc.json | 1 + packages/mfe-sidebar/src/App.tsx | 39 +++++++++++++++++- .../mfe-sidebar/src/components/Sidebar.tsx | 41 ++++--------------- packages/mfe-sidebar/tsconfig.json | 3 +- 4 files changed, 49 insertions(+), 35 deletions(-) diff --git a/packages/eslint-config/src/eslintrc.json b/packages/eslint-config/src/eslintrc.json index 31971a6..005da85 100644 --- a/packages/eslint-config/src/eslintrc.json +++ b/packages/eslint-config/src/eslintrc.json @@ -24,6 +24,7 @@ "rules": { "react/react-in-jsx-scope": "off", "react/prop-types": "off", + "react/display-name": "off", "@typescript-eslint/no-empty-function": "warn" } } diff --git a/packages/mfe-sidebar/src/App.tsx b/packages/mfe-sidebar/src/App.tsx index e7d7d6e..fbaedef 100644 --- a/packages/mfe-sidebar/src/App.tsx +++ b/packages/mfe-sidebar/src/App.tsx @@ -1,5 +1,42 @@ import { Sidebar } from "./components/Sidebar"; +import { useState, useEffect, useCallback } from "react"; export const App = () => { - return ; + const [sidebarRef, setSidebarRef] = useState(); + + const callbackRef = useCallback((el) => { + if (el) { + setSidebarRef(el); + } + }, []); + + const [isOpen, setIsOpen] = useState( + JSON.parse(localStorage.getItem("typer:sidebar-open")) + ); + + const onToggle = () => { + setIsOpen((prev) => !prev); + }; + + useEffect(() => { + const observer = new ResizeObserver((entries) => { + if (!entries[0]?.contentRect) { + return; + } + const event = new CustomEvent("typer:sidebar-resize", { + detail: { + width: entries[0].contentRect.width, + }, + }); + window.dispatchEvent(event); + }); + + observer.observe(sidebarRef); + }, [sidebarRef]); + + useEffect(() => { + localStorage.setItem("typer:sidebar-open", isOpen); + }, [isOpen]); + + return ; }; diff --git a/packages/mfe-sidebar/src/components/Sidebar.tsx b/packages/mfe-sidebar/src/components/Sidebar.tsx index f55aaa5..8577e3a 100644 --- a/packages/mfe-sidebar/src/components/Sidebar.tsx +++ b/packages/mfe-sidebar/src/components/Sidebar.tsx @@ -1,43 +1,18 @@ -import { useEffect, useState, useRef } from "react"; +import { forwardRef } from "react"; import clsx from "clsx"; import { ChevronLeftIcon, PencilAltIcon, NewspaperIcon } from "@mfe/styleguide"; -export const Sidebar = () => { - const [isOpen, setIsOpen] = useState( - JSON.parse(localStorage.getItem("typer:sidebar-open")) - ); - - const onToggle = () => { - setIsOpen(!isOpen); - }; - - const wrapperRef = useRef(); - - useEffect(() => { - localStorage.setItem("typer:sidebar-open", isOpen); - }, [isOpen]); - - useEffect(() => { - const observer = new ResizeObserver((entries) => { - if (!entries[0]?.contentRect) { - return; - } - const event = new CustomEvent("typer:sidebar-resize", { - detail: { - width: entries[0].contentRect.width, - }, - }); - window.dispatchEvent(event); - }); - - observer.observe(wrapperRef.current); - }, []); +type Props = { + isOpen: boolean; + onToggle: () => void; +}; +export const Sidebar = forwardRef(({ isOpen, onToggle }: Props, ref) => { return (
); -}; +}); diff --git a/packages/mfe-sidebar/tsconfig.json b/packages/mfe-sidebar/tsconfig.json index a224293..fb91f14 100644 --- a/packages/mfe-sidebar/tsconfig.json +++ b/packages/mfe-sidebar/tsconfig.json @@ -1,5 +1,6 @@ { "compilerOptions": { - "jsx": "react-jsx" + "jsx": "react-jsx", + "esModuleInterop": true } }