This file contains 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
Vue.component('accordion-panel', { | |
inject: ['accordionItemState'], | |
template: ` | |
<div class="accordion-panel" :class="{ expanded: accordionItemState.expanded }"> | |
<slot></slot> | |
</div> | |
` | |
}) |
This file contains 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
Vue.component('accordion-header', { | |
inject: ['accordionItemState'], | |
template: ` | |
<h2 class="accordion-header"> | |
<button @click="accordionItemState.expanded = !accordionItemState.expanded"> | |
{{ accordionItemState.expanded ? '▼' : '►' }} | |
<slot></slot> | |
</button> | |
</h2> |
This file contains 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
Vue.component('accordion-item', { | |
data () { | |
return { | |
sharedState: { | |
expanded: false | |
} | |
} | |
}, | |
provide () { |
This file contains 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 AccordionPanel = (props) => { | |
return ( | |
<AccordionItemContext.Consumer> | |
{({ expanded }) => <div className={"accordion-panel " + (expanded ? 'expanded' : '')}>{props.children}</div>} | |
</AccordionItemContext.Consumer> | |
) | |
} |
This file contains 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 AccordionHeader = (props) => { | |
return ( | |
<AccordionItemContext.Consumer> | |
{({ expanded, toggleExpansion }) => ( | |
<h2 className="accordion-header"> | |
<button onClick={toggleExpansion}> | |
{ expanded ? '▼ ' : '► ' } | |
{ props.children } | |
</button> | |
</h2> |
This file contains 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
class AccordionItem extends React.Component { | |
constructor (props) { | |
super(props) | |
this.toggleExpansion = () => { | |
this.setState({ expanded: !this.state.expanded }) | |
} | |
this.state = { | |
expanded: false, |
This file contains 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 AccordionItemContext = React.createContext({ | |
expanded: false, | |
toggleExpansion: () => {} | |
}); |
This file contains 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> | |
<AccordionItem render={({expanded}) => ( | |
<AccordionHeader expanded={expanded}> | |
Header content | |
</AccordionHeader> | |
<AccordionPanel expanded={expanded}> | |
Panel content | |
</AccordionPanel> | |
)} | |
/> |
This file contains 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> | |
<AccordionItem> | |
<AccordionHeader>Header content</AccordionHeader> | |
<AccordionPanel>Panel content</AccordionPanel> | |
</AccordionItem> | |
</Accordion> |
This file contains 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
class App extends React.Component { | |
constructor(props) { | |
super(props) | |
} | |
render() { | |
return ( | |
<div className="wrapper"> | |
<ObserveDimensions | |
render={({width, height}) => ( |
NewerOlder