Skip to content

Commit

Permalink
refactor: convert all PropFilter usage to transient props
Browse files Browse the repository at this point in the history
  • Loading branch information
jwinr committed Sep 19, 2024
1 parent 4206183 commit 975d6c4
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 95 deletions.
15 changes: 6 additions & 9 deletions src/components/Elements/Pagination.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,12 +17,10 @@ const PaginationWrapper = styled.div`
`

interface PaginationButtonProps {
isActive: boolean
$isActive: boolean
}

const PaginationButton = styled(
PropFilter('button')(['isActive'])
)<PaginationButtonProps>`
const PaginationButton = styled.button<PaginationButtonProps>`
position: relative;
border: none;
display: flex;
Expand All @@ -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:
Expand All @@ -60,8 +57,8 @@ const PaginationButton = styled(
opacity: 0;
}
${(props) =>
props.isActive &&
${($isActive) =>
$isActive &&
css`
&:after {
transition:
Expand Down Expand Up @@ -170,7 +167,7 @@ const Pagination: React.FC<PaginationProps> = ({
<PaginationButton
key={i}
onClick={() => handlePageChange(i)}
isActive={i === currentPage}
$isActive={i === currentPage}
>
{i}
</PaginationButton>
Expand Down
11 changes: 4 additions & 7 deletions src/components/Homepage/NewsletterSignup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,14 +23,14 @@ const NewsletterSubtitle = styled.p`
`

interface NewsletterFormProps {
isVisible: boolean
$isVisible: boolean
}

const NewsletterForm = styled(formFilter(['isVisible']))<NewsletterFormProps>`
const NewsletterForm = styled.form<NewsletterFormProps>`
display: flex;
justify-content: center;
gap: 10px;
opacity: ${(props) => (props.isVisible ? 1 : 0)};
opacity: ${($isVisible) => ($isVisible ? 1 : 0)};
transition: opacity 0.5s ease;
`

Expand Down Expand Up @@ -103,7 +100,7 @@ const NewsletterSignup: React.FC = () => {
Thanks for subscribing to our newsletter!
</SuccessMessage>
) : (
<NewsletterForm isVisible={!submitted} onSubmit={handleSubmit}>
<NewsletterForm $isVisible={!submitted} onSubmit={handleSubmit}>
<NewsletterInput
type="email"
placeholder="Enter your email"
Expand Down
13 changes: 5 additions & 8 deletions src/components/Loaders/LoaderSpin.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react'
import styled, { keyframes } from 'styled-components'
import PropFilter from 'src/utils/PropFilter'

const spin = keyframes`
0% {
Expand All @@ -21,17 +20,15 @@ const spinner = keyframes`
`

interface LoaderSpinnerProps {
isLoading: boolean
$isLoading: boolean
}

const LoaderSpinner = styled(
PropFilter('div')(['isLoading'])
)<LoaderSpinnerProps>`
const LoaderSpinner = styled.div<LoaderSpinnerProps>`
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 {
Expand Down Expand Up @@ -64,7 +61,7 @@ interface LoaderSpinProps {
}

const LoaderSpin: React.FC<LoaderSpinProps> = ({ isLoading }) => (
<LoaderSpinner isLoading={isLoading}>
<LoaderSpinner $isLoading={isLoading}>
<InnerDiv>
<svg preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24" x="0" y="0">
<circle
Expand Down
18 changes: 9 additions & 9 deletions src/components/Products/FilterPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React, { useState, useEffect, useRef, useCallback } from 'react'
import ReactDOM from 'react-dom'
import styled, { keyframes } from 'styled-components'
import Checkbox from '@/components/Elements/Checkbox'
import PropFilter from 'src/utils/PropFilter'
import Cancel from '@/public/images/icons/cancel.svg'
import Accordion from '@/components/Elements/Accordion'
import AccordionItem from '@/components/Elements/AccordionItem'
Expand Down Expand Up @@ -45,8 +44,8 @@ const slideOut = keyframes`
}
`

const PanelContainer = styled(PropFilter('div')(['isOpen']))<{
isOpen: boolean
const PanelContainer = styled.div<{
$isOpen: boolean
}>`
position: absolute;
top: 0;
Expand All @@ -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`
Expand Down Expand Up @@ -235,10 +235,10 @@ const FilterPanel: React.FC<FilterPanelProps> = ({
{isMounted &&
shouldRender &&
ReactDOM.createPortal(
<Backdrop isOpen={isOpen} onClick={onClose}>
<Backdrop $isOpen={isOpen} onClick={onClose}>
<PanelContainer
ref={panelRef}
isOpen={isOpen}
$isOpen={isOpen}
onClick={(e) => e.stopPropagation()}
>
<Header>
Expand Down
17 changes: 8 additions & 9 deletions src/components/Products/VoteButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
`

Expand Down Expand Up @@ -97,10 +96,10 @@ const VoteButton: React.FC<VoteButtonProps> = ({
}}
disabled={disabled || loading}
>
<ThumbsIcon loading={loading}>
<ThumbsIcon $loading={loading}>
{type === 'upvote' ? <LiaThumbsUpSolid /> : <LiaThumbsDownSolid />}
</ThumbsIcon>
<Count loading={loading}>{voteCount}</Count>
<Count $loading={loading}>{voteCount}</Count>
<LoaderSpin isLoading={loading} />
</Button>
)
Expand Down
32 changes: 14 additions & 18 deletions src/components/Shopping/AddToFavoritesButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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% {
Expand Down Expand Up @@ -42,23 +38,23 @@ const Button = styled.button`
`

interface IconProps {
loading?: boolean
isAdding?: boolean
$loading?: boolean
$isAdding?: boolean
}

const IconOutline = styled(FilteredLiaHeart)<IconProps>`
${({ loading, isAdding }) =>
loading &&
isAdding &&
const IconOutline = styled(LiaHeart)<IconProps>`
${({ $loading, $isAdding }) =>
$loading &&
$isAdding &&
css`
animation: ${loadingAnimation} 0.5s ease-in-out;
`}
`

const IconFilled = styled(FilteredLiaHeartSolid)<IconProps>`
${({ loading, isAdding }) =>
loading &&
isAdding &&
const IconFilled = styled(LiaHeartSolid)<IconProps>`
${({ $loading, $isAdding }) =>
$loading &&
$isAdding &&
css`
animation: ${loadingAnimation} 0.5s ease-in-out;
`}
Expand Down Expand Up @@ -174,14 +170,14 @@ const AddToFavoritesButton: React.FC<AddToFavoritesButtonProps> = ({
>
{added ? (
<IconFilled
loading={loading}
isAdding={isAdding}
$loading={loading}
$isAdding={isAdding}
aria-hidden="true"
/>
) : (
<IconOutline
loading={loading}
isAdding={isAdding}
$loading={loading}
$isAdding={isAdding}
aria-hidden="true"
/>
)}
Expand Down
35 changes: 0 additions & 35 deletions src/utils/PropFilter.ts

This file was deleted.

0 comments on commit 975d6c4

Please sign in to comment.