-
-
Save isaacplmann/aefeb31d2c47585dbffda60c48ae41b7 to your computer and use it in GitHub Desktop.
A Render Prop by Any Other Name: React
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {Provider, Consumer: ThemeConsumer} = React.createContext() | |
class ThemeProvider extends React.Component { | |
setTheme = theme => this.setState({theme}) | |
state = {theme: 'dark', setTheme: this.setTheme} | |
render() { | |
return <Provider value={this.state} {...this.props} /> | |
} | |
} | |
function AccordionButton({isOpen, ...props}) { | |
return ( | |
<ThemeConsumer> | |
{({theme}) => ( | |
<button | |
className={`accordion accordion-${theme} ${isOpen ? 'open' : 'closed'}`} | |
{...props} | |
/> | |
)} | |
</ThemeConsumer> | |
) | |
} | |
<ThemeProvider> | |
... | |
... | |
<AccordionButton | |
{...getButtonProps({isOpen: openIndexes.includes(index)})} | |
> | |
{`${item.title} ${openIndexes.includes(index) ? 'π' : 'π'}`} | |
</AccordionButton> | |
... | |
... | |
</ThemeProvider> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Accordion> | |
{({openIndexes, getButtonProps}) => ( | |
<div> | |
{items.map((item, index) => ( | |
<AccordionItem key={item.title}> | |
<AccordionButton | |
{...getButtonProps({isOpen: openIndexes.includes(index)})} | |
> | |
{`${item.title} ${openIndexes.includes(index) ? 'π' : 'π'}`} | |
</AccordionButton> | |
<AccordionContents isOpen={openIndexes.includes(index)}> | |
{item.contents} | |
</AccordionContents> | |
</AccordionItem> | |
))} | |
</div> | |
)} | |
</Accordion> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
preventClose, // prevents the state from having all open indexes closed | |
single, // allows only one open index at a time | |
combineReducers, // accepts any number of reducers and calls each of them in turn | |
} from './reducers'; | |
// composes the Accordion into a component that uses the logic for a Tabs UI | |
const Tabs = ({stateReducer = (state, changes) => changes, ...props}) => ( | |
<Accordion | |
stateReducer={combineReducers(single, preventClose, stateReducer)} | |
{...props} | |
/> | |
) | |
// usage: | |
<Tabs> | |
{({openIndexes, getButtonProps}) => ( | |
<div> | |
<TabButtons> | |
... | |
</TabButtons> | |
<TabItems> | |
... | |
</TabItems> | |
</div> | |
)} | |
</Tabs> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment