Skip to content
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

#3 Compound Components #17

Open
wants to merge 3 commits into
base: 02
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
63 changes: 47 additions & 16 deletions showcase/src/patterns/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import React, { useState, useCallback, useLayoutEffect } from 'react'
import React, {
useState,
useCallback,
useLayoutEffect,
useContext,
useMemo,
createContext
} from 'react'

import mojs from 'mo-js'
import { generateRandomNumber } from '../utils/generateRandomNumber'
Expand Down Expand Up @@ -118,7 +125,10 @@ const initialState = {
isClicked: false
}

const MediumClap = () => {
const MediumClapContext = createContext()
const { Provider } = MediumClapContext

const MediumClap = ({ children }) => {
const MAXIMUM_USER_CLAP = 50
const [clapState, setClapState] = useState(initialState)
const { count, countTotal, isClicked } = clapState
Expand Down Expand Up @@ -151,17 +161,25 @@ const MediumClap = () => {
})
}

const memoizedValue = useMemo(
() => ({
...clapState,
setRef
}),
[clapState, setRef]
)

return (
<button
ref={setRef}
data-refkey='clapRef'
className={styles.clap}
onClick={handleClapClick}
>
<ClapIcon isClicked={isClicked} />
<ClapCount count={count} setRef={setRef} />
<CountTotal countTotal={countTotal} setRef={setRef} />
</button>
<Provider value={memoizedValue}>
<button
ref={setRef}
data-refkey='clapRef'
className={styles.clap}
onClick={handleClapClick}
>
{children}
</button>
</Provider>
)
}

Expand All @@ -170,7 +188,8 @@ const MediumClap = () => {
Smaller Component used by <MediumClap />
==================================== **/

const ClapIcon = ({ isClicked }) => {
const ClapIcon = () => {
const { isClicked } = useContext(MediumClapContext)
return (
<span>
<svg
Expand All @@ -185,29 +204,41 @@ const ClapIcon = ({ isClicked }) => {
</span>
)
}
const ClapCount = ({ count, setRef }) => {
const ClapCount = () => {
const { count, setRef } = useContext(MediumClapContext)
return (
<span ref={setRef} data-refkey='clapCountRef' className={styles.count}>
+{count}
</span>
)
}
const CountTotal = ({ countTotal, setRef }) => {
const CountTotal = () => {
const { countTotal, setRef } = useContext(MediumClapContext)
return (
<span ref={setRef} data-refkey='clapTotalRef' className={styles.total}>
{countTotal}
</span>
)
}

MediumClap.Icon = ClapIcon
MediumClap.Count = ClapCount
MediumClap.Total = CountTotal

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MediumClap.Icon = ClapIcon
MediumClap.Count = ClapCount
MediumClap.Total = CountTotal

This is completely optional. You may export the child components directly - without adding them as publicly accessible properties on the Parent component.

/** ====================================
* 🔰USAGE
Below's how a potential user
may consume the component API
==================================== **/

const Usage = () => {
return <MediumClap />
return (
<MediumClap>
<MediumClap.Icon />
<MediumClap.Total />
<MediumClap.Count />
</MediumClap>
)
}

export default Usage