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

feat(docs): improve ux #2820

Merged
merged 1 commit into from
May 21, 2018
Merged
Show file tree
Hide file tree
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
102 changes: 43 additions & 59 deletions docs/app/Components/ComponentDoc/ComponentExample/ComponentExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import copyToClipboard from 'copy-to-clipboard'

import {
exampleContext,
repoURL,
scrollToAnchor,
examplePathToHash,
getFormattedHash,
repoURL,
scrollToAnchor,
} from 'docs/app/utils'
import { Divider, Grid, Menu, Visibility } from 'src'
import Editor from 'docs/app/Components/Editor/Editor'
Expand All @@ -34,12 +34,6 @@ const babelConfig = {
],
}

const headerColumnStyle = {
// provide room for absolutely positions toggle code icons
minHeight: '4em',
paddingRight: '7em',
}

const childrenStyle = {
paddingTop: 0,
maxWidth: '50rem',
Expand All @@ -52,11 +46,17 @@ const errorStyle = {
background: '#fff2f2',
}

const controlsWrapperStyle = {
minHeight: '3rem',
}

/**
* Renders a `component` and the raw `code` that produced it.
* Allows toggling the the raw `code` code block.
*/
class ComponentExample extends PureComponent {
state = {}

static contextTypes = {
onPassed: PropTypes.func,
}
Expand All @@ -74,20 +74,35 @@ class ComponentExample extends PureComponent {

componentWillMount() {
const { examplePath } = this.props
const sourceCode = this.getOriginalSourceCode()

this.anchorName = examplePathToHash(examplePath)

const exampleElement = this.renderOriginalExample()
const markup = renderToStaticMarkup(exampleElement)

this.setState({
exampleElement,
handleMouseLeave: this.handleMouseLeave,
handleMouseMove: this.handleMouseMove,
showCode: this.isActiveHash(),
sourceCode,
markup,
sourceCode: this.getOriginalSourceCode(),
markup: renderToStaticMarkup(exampleElement),
})
}

componentWillReceiveProps(nextProps) {
// deactivate examples when switching from one to the next
if (
this.isActiveHash() &&
this.isActiveState() &&
this.props.location.hash !== nextProps.location.hash
) {
this.clearActiveState()
}
}

clearActiveState = () => {
this.setState({
showCode: false,
showHTML: false,
})
}

Expand Down Expand Up @@ -116,10 +131,7 @@ class ComponentExample extends PureComponent {

history.replace(location.pathname)

this.setState({
showCode: false,
showHTML: false,
})
this.clearActiveState()
}

handleDirectLinkClick = () => {
Expand Down Expand Up @@ -304,45 +316,19 @@ class ComponentExample extends PureComponent {
}

setGitHubHrefs = () => {
const { examplePath, location } = this.props
const { examplePath } = this.props

if (this.ghEditHref && this.ghBugHref) return

// get component name from file path:
// elements/Button/Types/ButtonButtonExample
const pathParts = examplePath.split(__PATH_SEP__)
const componentName = pathParts[1]
const filename = pathParts[pathParts.length - 1]

this.ghEditHref = [
`${repoURL}/edit/master/docs/app/Examples/${examplePath}.js`,
`?message=docs(${filename}): your description`,
].join('')

this.ghBugHref = [
`${repoURL}/issues/new?`,
_.map(
{
title: `fix(${componentName}): your description`,
body: [
'**Steps to Reproduce**',
'1. Do something',
'2. Do something else.',
'',
'**Expected**',
`The ${componentName} should do this`,
'',
'**Result**',
`The ${componentName} does not do this`,
'',
'**Testcase**',
`If the docs show the issue, use: ${location.href}`,
'Otherwise, fork this to get started: http://codepen.io/levithomason/pen/ZpBaJX',
].join('\n'),
},
(val, key) => `${key}=${encodeURIComponent(val)}`,
).join('&'),
].join('')
}

renderJSXControls = () => {
Expand Down Expand Up @@ -376,14 +362,6 @@ class ComponentExample extends PureComponent {
href={this.ghEditHref}
target='_blank'
/>
<Menu.Item
active={!!error} // to show the color
color={color}
icon='bug'
content='Issue'
href={this.ghBugHref}
target='_blank'
/>
</Menu>
</Divider>
)
Expand Down Expand Up @@ -441,7 +419,7 @@ class ComponentExample extends PureComponent {
}

render() {
const { children, description, suiVersion, title } = this.props
const { children, description, location, suiVersion, title } = this.props
const {
handleMouseLeave,
handleMouseMove,
Expand All @@ -453,9 +431,13 @@ class ComponentExample extends PureComponent {

const isActive = this.isActiveHash() || this.isActiveState()

const isInFocus = !location.hash || (location.hash && (this.isActiveHash() || isHovering))

const exampleStyle = {
position: 'relative',
transition: 'box-shadow 200ms, background 200ms',
transition: 'box-shadow 200ms, background 200ms, opacity 200ms, filter 200ms',
opacity: isInFocus ? 1 : 0.4,
filter: isInFocus ? 'grayscale(0)' : 'grayscale(1)',
...(isActive
? {
background: '#fff',
Expand All @@ -479,14 +461,14 @@ class ComponentExample extends PureComponent {
style={exampleStyle}
>
<Grid.Row>
<Grid.Column style={headerColumnStyle} width={12}>
<Grid.Column width={12}>
<ComponentExampleTitle
description={description}
title={title}
suiVersion={suiVersion}
/>
</Grid.Column>
<Grid.Column textAlign='right' width={4}>
<Grid.Column textAlign='right' width={4} style={controlsWrapperStyle}>
<ComponentControls
anchorName={this.anchorName}
onCopyLink={this.handleDirectLinkClick}
Expand All @@ -499,9 +481,11 @@ class ComponentExample extends PureComponent {
</Grid.Column>
</Grid.Row>

<Grid.Row columns={1}>
{children && <Grid.Column style={childrenStyle}>{children}</Grid.Column>}
</Grid.Row>
{children && (
<Grid.Row columns={1}>
<Grid.Column style={childrenStyle}>{children}</Grid.Column>
</Grid.Row>
)}

<Grid.Row columns={1}>
<Grid.Column className={`rendered-example ${this.getKebabExamplePath()}`}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import { pure } from 'docs/app/HOC'
const titleStyle = {
margin: 0,
}
const descriptionStyle = {
maxWidth: '50rem',
}

const ComponentExampleTitle = ({ description, title, suiVersion }) => (
<div>
Expand All @@ -27,7 +24,7 @@ const ComponentExampleTitle = ({ description, title, suiVersion }) => (
)}
</Header>
)}
{description && <p style={descriptionStyle}>{description}</p>}
{description && <p>{description}</p>}
</div>
)

Expand Down
4 changes: 2 additions & 2 deletions docs/app/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export * from './constants'
export exampleContext from './exampleContext'
export examplePathToHash from './examplePathToHash'
export getComponentGroup from './getComponentGroup'
export getFormattedHash from './getFormattedHash'
export getSeeItems from './getSeeItems'
export scrollToAnchor from './scrollToAnchor'
export examplePathToHash from './examplePathToHash'
export parentComponents from './parentComponents'
export scrollToAnchor from './scrollToAnchor'
21 changes: 12 additions & 9 deletions docs/app/utils/scrollToAnchor.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
const mathSign = Math.sign || function (x) {
const val = +x
const mathSign =
Math.sign ||
function mathSign(x) {
const val = +x

if (val === 0 || isNaN(val)) return val
return val > 0 ? 1 : -1
}
if (val === 0 || isNaN(val)) return val
return val > 0 ? 1 : -1
}

const scrollToAnchor = (lastOffsetY) => {
const scrollToAnchor = (lastOffsetY, lastAcceleration = 0.1) => {
const anchor = location.hash && document.querySelector(location.hash)
const offsetY = window.scrollY || window.pageYOffset

// no scroll to target, stop
if (!anchor) return

const elementTop = Math.round(anchor.getBoundingClientRect().top)
const scrollStep = Math.ceil((Math.abs(elementTop / 8))) * mathSign(elementTop)
const scrollStep = Math.ceil(Math.abs(elementTop / 8)) * mathSign(elementTop)
const acceleration = Math.min(1, (lastAcceleration * 100) ** 1.1 / 100)

// if our last step was not applied, stop
// we've either hit the top, bottom, or arrived at the element
if (lastOffsetY === offsetY) return

// more scrolling to do!
scrollBy(0, scrollStep)
requestAnimationFrame(() => scrollToAnchor(offsetY))
scrollBy(0, scrollStep * acceleration)
requestAnimationFrame(() => scrollToAnchor(offsetY, acceleration))
}

export default scrollToAnchor
5 changes: 5 additions & 0 deletions docs/src/examples/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"react/prop-types": 0
}
}