Skip to content

#08: Prop Getters #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 07
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 68 additions & 54 deletions showcase/src/patterns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ const INIT_STATE = {
isClicked: false
}

const callFnsInSequence = (...fns) => (...args) =>
fns.forEach(fn => fn && fn(...args))

const useClapState = ({ initialState = INIT_STATE } = {}) => {
const [clapState, setClapState] = useState(initialState)
const { count, countTotal } = clapState
Expand All @@ -147,22 +150,24 @@ const useClapState = ({ initialState = INIT_STATE } = {}) => {
[count, countTotal]
)

const togglerProps = {
onClick: handleClapClick,
'aria-pressed': clapState.isClicked
}
const getTogglerProps = ({ onClick, ...otherProps } = {}) => ({
onClick: callFnsInSequence(handleClapClick, onClick),
'aria-pressed': clapState.isClicked,
...otherProps
})

const counterProps = {
const getCounterProps = ({ ...otherProps }) => ({
count,
'aria-valuemax': MAX_CLAP,
'aria-valuemin': 0,
'aria-valuenow': count
}
'aria-valuenow': count,
...otherProps
})

return {
clapState,
togglerProps,
counterProps
getTogglerProps,
getCounterProps
}
}

Expand Down Expand Up @@ -200,50 +205,6 @@ const useDOMRef = () => {
return [DOMRef, setRef]
}

/** ====================================
* 🔰 MediumClap
==================================== **/

const MediumClap = () => {
const { clapState, togglerProps, counterProps } = useClapState()
const { count, countTotal, isClicked } = clapState

const [
{ clapContainerRef, clapCountRef, countTotalRef },
setRef
] = useDOMRef()

const animationTimeline = useClapAnimation({
duration: 300,
bounceEl: clapCountRef,
fadeEl: countTotalRef,
burstEl: clapContainerRef
})

useEffectAfterMount(
() => {
animationTimeline.replay()
},
[count]
)

return (
<ClapContainer
ref={setRef}
data-refkey='clapContainerRef'
{...togglerProps}
>
<ClapIcon isClicked={isClicked} />
<ClapCount ref={setRef} data-refkey='clapCountRef' {...counterProps} />
<CountTotal
ref={setRef}
data-refkey='countTotalRef'
countTotal={countTotal}
/>
</ClapContainer>
)
}

/** ====================================
* 🔰SubComponents
Smaller Component used by <MediumClap />
Expand Down Expand Up @@ -325,7 +286,60 @@ const CountTotal = forwardRef(
==================================== **/

const Usage = () => {
return <MediumClap />
const { clapState, getTogglerProps, getCounterProps } = useClapState()
const { count, countTotal, isClicked } = clapState

const [
{ clapContainerRef, clapCountRef, countTotalRef },
setRef
] = useDOMRef()

const animationTimeline = useClapAnimation({
duration: 300,
bounceEl: clapCountRef,
fadeEl: countTotalRef,
burstEl: clapContainerRef
})

const onClick = () => {
animationTimeline.replay()
}

return (
<div style={{ textAlign: 'center' }}>
<ClapContainer
ref={setRef}
data-refkey='clapContainerRef'
{...getTogglerProps({
'data-testId': '#grabThis',
onClick
})}
>
<ClapIcon isClicked={isClicked} />
<ClapCount
ref={setRef}
data-refkey='clapCountRef'
{...getCounterProps()}
/>
<CountTotal
ref={setRef}
data-refkey='countTotalRef'
countTotal={countTotal}
/>
</ClapContainer>
<section
style={{
borderBottom: '1px solid #bdc3c7',
color: '#27ae60',
marginTop: '30px',
padding: '10px 20px',
borderRadius: '20px'
}}
>
{!isClicked ? 'No recommendations :(' : `Recommended ${count} times 🔥`}
</section>
</div>
)
}

export default Usage