From 975d6c4c7f4d88c553685d1305f288490a3016e2 Mon Sep 17 00:00:00 2001 From: jwinr Date: Thu, 19 Sep 2024 17:42:13 -0400 Subject: [PATCH] refactor: convert all PropFilter usage to transient props --- src/components/Elements/Pagination.tsx | 15 ++++---- src/components/Homepage/NewsletterSignup.tsx | 11 +++--- src/components/Loaders/LoaderSpin.tsx | 13 +++---- src/components/Products/FilterPanel.tsx | 18 +++++----- src/components/Products/VoteButton.tsx | 17 +++++---- .../Shopping/AddToFavoritesButton.tsx | 32 ++++++++--------- src/utils/PropFilter.ts | 35 ------------------- 7 files changed, 46 insertions(+), 95 deletions(-) delete mode 100755 src/utils/PropFilter.ts diff --git a/src/components/Elements/Pagination.tsx b/src/components/Elements/Pagination.tsx index df69a28..d2aded0 100644 --- a/src/components/Elements/Pagination.tsx +++ b/src/components/Elements/Pagination.tsx @@ -1,7 +1,6 @@ import React, { useEffect } from 'react' import styled, { css } from 'styled-components' import { IoIosArrowBack, IoIosArrowForward } from 'react-icons/io' -import PropFilter from 'src/utils/PropFilter' const PaginationWrapper = styled.div` display: flex; @@ -18,12 +17,10 @@ const PaginationWrapper = styled.div` ` interface PaginationButtonProps { - isActive: boolean + $isActive: boolean } -const PaginationButton = styled( - PropFilter('button')(['isActive']) -)` +const PaginationButton = styled.button` position: relative; border: none; display: flex; @@ -33,7 +30,7 @@ const PaginationButton = styled( padding: 0.625rem 0.75rem; color: var(--sc-color-carnation); font-weight: 600; - cursor: ${(props) => (props.isActive ? 'default' : 'pointer')}; + cursor: ${($isActive) => ($isActive ? 'default' : 'pointer')}; &:hover::after { transition: @@ -60,8 +57,8 @@ const PaginationButton = styled( opacity: 0; } - ${(props) => - props.isActive && + ${($isActive) => + $isActive && css` &:after { transition: @@ -170,7 +167,7 @@ const Pagination: React.FC = ({ handlePageChange(i)} - isActive={i === currentPage} + $isActive={i === currentPage} > {i} diff --git a/src/components/Homepage/NewsletterSignup.tsx b/src/components/Homepage/NewsletterSignup.tsx index e620215..0d2c575 100644 --- a/src/components/Homepage/NewsletterSignup.tsx +++ b/src/components/Homepage/NewsletterSignup.tsx @@ -2,11 +2,8 @@ import React, { useState, ChangeEvent, FormEvent } from 'react' import styled, { keyframes } from 'styled-components' -import PropFilter from 'src/utils/PropFilter' import Button from '@/components/Elements/Button' -const formFilter = PropFilter('form') - const NewsletterContainer = styled.div` text-align: center; padding: 20px; @@ -26,14 +23,14 @@ const NewsletterSubtitle = styled.p` ` interface NewsletterFormProps { - isVisible: boolean + $isVisible: boolean } -const NewsletterForm = styled(formFilter(['isVisible']))` +const NewsletterForm = styled.form` display: flex; justify-content: center; gap: 10px; - opacity: ${(props) => (props.isVisible ? 1 : 0)}; + opacity: ${($isVisible) => ($isVisible ? 1 : 0)}; transition: opacity 0.5s ease; ` @@ -103,7 +100,7 @@ const NewsletterSignup: React.FC = () => { Thanks for subscribing to our newsletter! ) : ( - + ` +const LoaderSpinner = styled.div` position: absolute; justify-content: center; align-items: center; - opacity: ${({ isLoading }) => (isLoading ? 1 : 0)}; - display: ${({ isLoading }) => (isLoading ? 'flex' : 'none')}; + opacity: ${({ $isLoading }) => ($isLoading ? 1 : 0)}; + display: ${({ $isLoading }) => ($isLoading ? 'flex' : 'none')}; transition: opacity 0.3s ease-in-out; & > div { @@ -64,7 +61,7 @@ interface LoaderSpinProps { } const LoaderSpin: React.FC = ({ isLoading }) => ( - + ` position: absolute; top: 0; @@ -61,23 +60,24 @@ const PanelContainer = styled(PropFilter('div')(['isOpen']))<{ overflow-y: hidden; display: flex; flex-direction: column; - animation: ${({ isOpen }) => (isOpen ? slideIn : slideOut)} 0.3s ease forwards; + animation: ${({ $isOpen }) => ($isOpen ? slideIn : slideOut)} 0.3s ease + forwards; @media (max-width: 768px) { width: auto; } ` -const Backdrop = styled(PropFilter('div')(['isOpen']))<{ isOpen: boolean }>` +const Backdrop = styled.div<{ $isOpen: boolean }>` position: fixed; inset: 0; background-color: rgba(51, 51, 51, 0.8); backdrop-filter: blur(4px); z-index: 300; height: 100vh; - opacity: ${({ isOpen }) => (isOpen ? '1' : '0')}; + opacity: ${({ $isOpen }) => ($isOpen ? '1' : '0')}; transition: opacity 0.3s ease-in-out; - pointer-events: ${({ isOpen }) => (isOpen ? 'auto' : 'none')}; + pointer-events: ${({ $isOpen }) => ($isOpen ? 'auto' : 'none')}; ` const CloseButton = styled.button` @@ -235,10 +235,10 @@ const FilterPanel: React.FC = ({ {isMounted && shouldRender && ReactDOM.createPortal( - + e.stopPropagation()} >
diff --git a/src/components/Products/VoteButton.tsx b/src/components/Products/VoteButton.tsx index b0c94e4..d28b9c4 100755 --- a/src/components/Products/VoteButton.tsx +++ b/src/components/Products/VoteButton.tsx @@ -2,7 +2,6 @@ import React, { useState, useEffect } from 'react' import styled from 'styled-components' import { LiaThumbsUpSolid, LiaThumbsDownSolid } from 'react-icons/lia' import LoaderSpin from '@/components/Loaders/LoaderSpin' -import PropFilter from 'src/utils/PropFilter' interface VoteButtonProps { reviewId: string @@ -41,18 +40,18 @@ const Button = styled.button` } ` -const ThumbsIcon = styled(PropFilter('div')(['loading']))<{ - loading: boolean +const ThumbsIcon = styled.div<{ + $loading: boolean }>` font-size: 22px; - opacity: ${({ loading }) => (loading ? 0.2 : 1)}; + opacity: ${({ $loading }) => ($loading ? 0.2 : 1)}; transition: transform 0.3s; ` -const Count = styled(PropFilter('span')(['loading']))<{ - loading: boolean +const Count = styled.span<{ + $loading: boolean }>` - opacity: ${({ loading }) => (loading ? 0.2 : 1)}; + opacity: ${({ $loading }) => ($loading ? 0.2 : 1)}; transition: transform 0.3s; ` @@ -97,10 +96,10 @@ const VoteButton: React.FC = ({ }} disabled={disabled || loading} > - + {type === 'upvote' ? : } - {voteCount} + {voteCount} ) diff --git a/src/components/Shopping/AddToFavoritesButton.tsx b/src/components/Shopping/AddToFavoritesButton.tsx index 9470aba..a522ca7 100644 --- a/src/components/Shopping/AddToFavoritesButton.tsx +++ b/src/components/Shopping/AddToFavoritesButton.tsx @@ -5,10 +5,6 @@ import { useRouter } from 'next/navigation' import { UserContext } from '@/context/UserContext' import { useFavorites } from '@/context/FavoritesContext' import Popover from '@/components/Elements/Popover' -import PropFilter from 'src/utils/PropFilter' - -const FilteredLiaHeart = PropFilter(LiaHeart)(['loading', 'isAdding']) -const FilteredLiaHeartSolid = PropFilter(LiaHeartSolid)(['loading', 'isAdding']) const loadingAnimation = keyframes` 0% { @@ -42,23 +38,23 @@ const Button = styled.button` ` interface IconProps { - loading?: boolean - isAdding?: boolean + $loading?: boolean + $isAdding?: boolean } -const IconOutline = styled(FilteredLiaHeart)` - ${({ loading, isAdding }) => - loading && - isAdding && +const IconOutline = styled(LiaHeart)` + ${({ $loading, $isAdding }) => + $loading && + $isAdding && css` animation: ${loadingAnimation} 0.5s ease-in-out; `} ` -const IconFilled = styled(FilteredLiaHeartSolid)` - ${({ loading, isAdding }) => - loading && - isAdding && +const IconFilled = styled(LiaHeartSolid)` + ${({ $loading, $isAdding }) => + $loading && + $isAdding && css` animation: ${loadingAnimation} 0.5s ease-in-out; `} @@ -174,14 +170,14 @@ const AddToFavoritesButton: React.FC = ({ > {added ? ( ) : ( )} diff --git a/src/utils/PropFilter.ts b/src/utils/PropFilter.ts deleted file mode 100755 index 4bb6877..0000000 --- a/src/utils/PropFilter.ts +++ /dev/null @@ -1,35 +0,0 @@ -/* eslint-disable @typescript-eslint/no-unsafe-argument */ -import React, { ElementType, forwardRef, PropsWithChildren } from 'react' - -// Helper function to filter out unwanted props and prevent the "unknown prop on DOM element" console warning - -/* Can be used like this; -const FilteredDiv = PropFilter("div")(["left"]); - -const Popover = styled(FilteredDiv)` -where 'div' is the element and 'left' is replaced with the prop name. -*/ - -const PropFilter = - (tag: T) => - (whitelist: string[]) => { - type Props = PropsWithChildren> - - return forwardRef, Props>(function PropFilterComponent( - { children, ...props }, - ref - ) { - // Create a new object to avoid mutation of props directly - const filteredProps = { ...props } as Record - - whitelist.forEach((prop) => { - if (prop in filteredProps) { - delete filteredProps[prop] - } - }) - - return React.createElement(tag, { ref, ...filteredProps }, children) - }) - } - -export default PropFilter