|
6 | 6 | */
|
7 | 7 |
|
8 | 8 | /* @flow */
|
9 |
| -import React, { Component } from 'react'; |
10 |
| -import { black } from '../styles'; |
| 9 | +import React, { useState, useContext } from 'react'; |
| 10 | +import { ThemeContext } from '../iframeScript'; |
11 | 11 |
|
12 | 12 | import type { Element as ReactElement } from 'react';
|
| 13 | +import type { Theme } from '../styles'; |
13 | 14 |
|
14 | 15 | const _collapsibleStyle = {
|
15 |
| - color: black, |
16 | 16 | cursor: 'pointer',
|
17 | 17 | border: 'none',
|
18 | 18 | display: 'block',
|
19 | 19 | width: '100%',
|
20 | 20 | textAlign: 'left',
|
21 |
| - background: '#fff', |
22 | 21 | fontFamily: 'Consolas, Menlo, monospace',
|
23 | 22 | fontSize: '1em',
|
24 | 23 | padding: '0px',
|
25 | 24 | lineHeight: '1.5',
|
26 | 25 | };
|
27 | 26 |
|
28 |
| -const collapsibleCollapsedStyle = { |
| 27 | +const collapsibleCollapsedStyle = (theme: Theme) => ({ |
29 | 28 | ..._collapsibleStyle,
|
| 29 | + color: theme.color, |
| 30 | + background: theme.background, |
30 | 31 | marginBottom: '1.5em',
|
31 |
| -}; |
| 32 | +}); |
32 | 33 |
|
33 |
| -const collapsibleExpandedStyle = { |
| 34 | +const collapsibleExpandedStyle = (theme: Theme) => ({ |
34 | 35 | ..._collapsibleStyle,
|
| 36 | + color: theme.color, |
| 37 | + background: theme.background, |
35 | 38 | marginBottom: '0.6em',
|
36 |
| -}; |
| 39 | +}); |
37 | 40 |
|
38 |
| -type Props = {| |
| 41 | +type CollapsiblePropsType = {| |
39 | 42 | children: ReactElement<any>[],
|
40 | 43 | |};
|
41 | 44 |
|
42 |
| -type State = {| |
43 |
| - collapsed: boolean, |
44 |
| -|}; |
45 |
| - |
46 |
| -class Collapsible extends Component<Props, State> { |
47 |
| - state = { |
48 |
| - collapsed: true, |
49 |
| - }; |
| 45 | +function Collapsible(props: CollapsiblePropsType) { |
| 46 | + const theme = useContext(ThemeContext); |
| 47 | + const [collapsed, setCollapsed] = useState(true); |
50 | 48 |
|
51 |
| - toggleCollapsed = () => { |
52 |
| - this.setState(state => ({ |
53 |
| - collapsed: !state.collapsed, |
54 |
| - })); |
| 49 | + const toggleCollapsed = () => { |
| 50 | + setCollapsed(!collapsed); |
55 | 51 | };
|
56 | 52 |
|
57 |
| - render() { |
58 |
| - const count = this.props.children.length; |
59 |
| - const collapsed = this.state.collapsed; |
60 |
| - return ( |
61 |
| - <div> |
| 53 | + const count = props.children.length; |
| 54 | + return ( |
| 55 | + <div> |
| 56 | + <button |
| 57 | + onClick={toggleCollapsed} |
| 58 | + style={ |
| 59 | + collapsed |
| 60 | + ? collapsibleCollapsedStyle(theme) |
| 61 | + : collapsibleExpandedStyle(theme) |
| 62 | + } |
| 63 | + > |
| 64 | + {(collapsed ? '▶' : '▼') + |
| 65 | + ` ${count} stack frames were ` + |
| 66 | + (collapsed ? 'collapsed.' : 'expanded.')} |
| 67 | + </button> |
| 68 | + <div style={{ display: collapsed ? 'none' : 'block' }}> |
| 69 | + {props.children} |
62 | 70 | <button
|
63 |
| - onClick={this.toggleCollapsed} |
64 |
| - style={ |
65 |
| - collapsed ? collapsibleCollapsedStyle : collapsibleExpandedStyle |
66 |
| - } |
| 71 | + onClick={toggleCollapsed} |
| 72 | + style={collapsibleExpandedStyle(theme)} |
67 | 73 | >
|
68 |
| - {(collapsed ? '▶' : '▼') + |
69 |
| - ` ${count} stack frames were ` + |
70 |
| - (collapsed ? 'collapsed.' : 'expanded.')} |
| 74 | + {`▲ ${count} stack frames were expanded.`} |
71 | 75 | </button>
|
72 |
| - <div style={{ display: collapsed ? 'none' : 'block' }}> |
73 |
| - {this.props.children} |
74 |
| - <button |
75 |
| - onClick={this.toggleCollapsed} |
76 |
| - style={collapsibleExpandedStyle} |
77 |
| - > |
78 |
| - {`▲ ${count} stack frames were expanded.`} |
79 |
| - </button> |
80 |
| - </div> |
81 | 76 | </div>
|
82 |
| - ); |
83 |
| - } |
| 77 | + </div> |
| 78 | + ); |
84 | 79 | }
|
85 | 80 |
|
86 | 81 | export default Collapsible;
|
0 commit comments