Skip to content

Commit

Permalink
refactor: Code review fixes in the CardFlip component
Browse files Browse the repository at this point in the history
  • Loading branch information
aesarine committed Apr 20, 2021
1 parent 6ea6913 commit e8cf311
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions src/CardFlip.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect, useMemo, useCallback } from 'react';
import React, { useMemo, useCallback, useState, useEffect } from 'react';
import { useOverrides } from '@quarkly/components';
import { Box, Image } from '@quarkly/widgets';

Expand Down Expand Up @@ -86,25 +86,19 @@ const FlipCard = ({

const [isFlipped, setFlipped] = useState(isFlippedProp);

const flipTrigger = useMemo(() => flipTriggerProp === 'Click', [
flipTriggerProp,
]);
const flipTrigger = flipTriggerProp === 'Click';

const flipDuration = useMemo(() => flipDurationProp.replace(/\s+/g, ''), [
flipDurationProp,
]);

const currentStyles = useMemo(() => flipStyles[flipDirectionProp], [
flipDirectionProp,
]);

const onClickFlip = useCallback(() => {
if (flipTrigger) setFlipped(!isFlipped);
}, [isFlipped]);
if (flipTrigger) setFlipped((prevFlipped) => !prevFlipped);
}, [flipTrigger]);

const onHoverFlip = useCallback(() => {
if (!flipTrigger) setFlipped(!isFlipped);
}, [isFlipped]);
if (!flipTrigger) setFlipped((prevFlipped) => !prevFlipped);
}, [flipTrigger]);

useEffect(() => {
setFlipped(isFlippedProp);
Expand All @@ -121,7 +115,7 @@ const FlipCard = ({
<Box
transition={`transform ${flipDuration}ms ${timingFunctionProp}`}
{...override('Flip Card Content')}
{...(isFlipped && currentStyles)}
{...(isFlipped && flipStyles[flipDirectionProp])}
padding-top={
aspectRatioProp !== 'auto'
? cardHeights[aspectRatioProp]
Expand All @@ -134,7 +128,7 @@ const FlipCard = ({
</Box>
<Box
{...override(`Flip Card Item`, `Flip Card Item Back`)}
{...currentStyles}
{...flipStyles[flipDirectionProp]}
>
{children}
</Box>
Expand Down

0 comments on commit e8cf311

Please sign in to comment.