Skip to content
Merged
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
35 changes: 27 additions & 8 deletions docs/src/modules/components/Demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import DemoFrame from 'docs/src/modules/components/DemoFrame';
import DemoLanguages from 'docs/src/modules/components/DemoLanguages';
import getDemoConfig from 'docs/src/modules/utils/getDemoConfig';
import compose from 'docs/src/modules/utils/compose';
import { getCookie } from 'docs/src/modules/utils/helpers';
import { ACTION_TYPES, CODE_VARIANTS } from 'docs/src/modules/constants';

function compress(object) {
Expand Down Expand Up @@ -95,8 +96,14 @@ class Demo extends React.Component {
state = {
anchorEl: null,
codeOpen: false,
demoHovered: false,
sourceHintSeen: false,
};

componentDidMount() {
this.setState({ sourceHintSeen: getCookie('sourceHintSeen') });
}

handleClickMore = event => {
this.setState({ anchorEl: event.currentTarget });
};
Expand Down Expand Up @@ -183,11 +190,17 @@ class Demo extends React.Component {
};

handleClickCodeOpen = () => {
document.cookie = `sourceHintSeen=true;path=/;max-age=31536000`;
this.setState(state => ({
codeOpen: !state.codeOpen,
sourceHintSeen: true,
}));
};

handleDemoHover = event => {
this.setState({ demoHovered: event.type === 'mouseenter' });
};

getDemoData = () => {
const { codeVariant, demo, githubLocation } = this.props;
if (codeVariant === CODE_VARIANTS.HOOK && demo.rawHooks) {
Expand Down Expand Up @@ -217,11 +230,13 @@ class Demo extends React.Component {

render() {
const { classes, codeVariant, demo, demoOptions } = this.props;
const { anchorEl, codeOpen } = this.state;
const { anchorEl, codeOpen, demoHovered, sourceHintSeen } = this.state;
const showSourceHint = demoHovered && !sourceHintSeen;
const category = demoOptions.demo;
const demoData = this.getDemoData();
const DemoComponent = demoData.js;
const sourceLanguage = demoData.codeVariant === CODE_VARIANTS.TS ? 'tsx' : 'jsx';
const Frame = demoOptions.iframe ? DemoFrame : React.Fragment;

return (
<div className={classes.root}>
Expand All @@ -236,12 +251,18 @@ class Demo extends React.Component {
onLanguageClick={this.handleCodeLanguageClick}
/>
<div>
<Tooltip title={codeOpen ? 'Hide the source' : 'Show the source'} placement="top">
<Tooltip
key={showSourceHint}
open={showSourceHint ? true : undefined}
title={codeOpen ? 'Hide the source' : 'Show the source'}
placement="top"
>
<IconButton
data-ga-event-category={category}
data-ga-event-action="expand"
onClick={this.handleClickCodeOpen}
aria-label={codeOpen ? 'Hide the source' : 'Show the source'}
color={demoHovered ? 'primary' : 'default'}
>
<CodeIcon />
</IconButton>
Expand Down Expand Up @@ -324,14 +345,12 @@ class Demo extends React.Component {
className={clsx(classes.demo, {
[classes.demoHiddenHeader]: demoOptions.hideHeader,
})}
onMouseEnter={this.handleDemoHover}
onMouseLeave={this.handleDemoHover}
>
{demoOptions.iframe ? (
<DemoFrame>
<DemoComponent />
</DemoFrame>
) : (
<Frame>
<DemoComponent />
)}
</Frame>
</div>
</div>
);
Expand Down