-
Notifications
You must be signed in to change notification settings - Fork 4.1k
feat(Transition): make duration
prop more advanced
#1967
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
import React from 'react' | ||
import { Message } from 'semantic-ui-react' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sort imports. |
||
|
||
import ComponentExample from 'docs/app/Components/ComponentDoc/ComponentExample' | ||
import ExampleSection from 'docs/app/Components/ComponentDoc/ExampleSection' | ||
|
||
import { Message } from 'semantic-ui-react' | ||
|
||
const TransitionTypesExamples = () => ( | ||
const TransitionExplorersExamples = () => ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo. |
||
<ExampleSection title='Explorers'> | ||
<ComponentExample | ||
title='Directional Animations' | ||
|
@@ -32,4 +31,4 @@ const TransitionTypesExamples = () => ( | |
</ExampleSection> | ||
) | ||
|
||
export default TransitionTypesExamples | ||
export default TransitionExplorersExamples |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import React, { Component } from 'react' | ||
import { Form, Grid, Image, Transition } from 'semantic-ui-react' | ||
|
||
export default class TransitionExampleDuration extends Component { | ||
state = { hide: 500, show: 500, visible: true } | ||
|
||
handleChange = (e, { name, value }) => this.setState({ [name]: value }) | ||
|
||
toggleVisibility = () => this.setState({ visible: !this.state.visible }) | ||
|
||
render() { | ||
const { hide, show, visible } = this.state | ||
|
||
return ( | ||
<Grid columns={2}> | ||
<Grid.Column as={Form}> | ||
<Form.Input | ||
label={`Hide duration: ${hide}ms `} | ||
min={100} | ||
max={5000} | ||
name='hide' | ||
onChange={this.handleChange} | ||
step={100} | ||
type='range' | ||
value={hide} | ||
/> | ||
<Form.Input | ||
label={`Show duration: ${show}ms `} | ||
min={100} | ||
max={5000} | ||
name='show' | ||
onChange={this.handleChange} | ||
step={100} | ||
type='range' | ||
value={show} | ||
/> | ||
<Form.Button content='Run' onClick={this.toggleVisibility} /> | ||
</Grid.Column> | ||
|
||
<Grid.Column> | ||
<Transition duration={{ hide, show }} visible={visible}> | ||
<Image centered size='small' src='/assets/images/leaves/3.png' /> | ||
</Transition> | ||
</Grid.Column> | ||
</Grid> | ||
) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import React from 'react' | ||
|
||
import ComponentExample from 'docs/app/Components/ComponentDoc/ComponentExample' | ||
import ExampleSection from 'docs/app/Components/ComponentDoc/ExampleSection' | ||
|
||
const TransitionUsageExamples = () => ( | ||
<ExampleSection title='Usage'> | ||
<ComponentExample | ||
title='Duration' | ||
description={( | ||
<span> | ||
Duration of the CSS transition animation can be defined separately for <code>hide</code> and <code>show</code> | ||
animations. | ||
</span> | ||
)} | ||
examplePath='modules/Transition/Usage/TransitionExampleDuration' | ||
/> | ||
</ExampleSection> | ||
) | ||
|
||
export default TransitionUsageExamples |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* Normalizes the duration of a transition. | ||
* @param {number|object} duration The value to normalize. | ||
* @param {'hide'|'show'} type The type of transition. | ||
* @returns {number} | ||
*/ | ||
export default (duration, type) => ( | ||
(typeof duration === 'number' || typeof duration === 'string') ? duration : duration[type] | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,13 +6,19 @@ import { cloneElement, Component } from 'react' | |
import { | ||
makeDebugger, | ||
META, | ||
normalizeTransitionDuration, | ||
SUI, | ||
useKeyOnly, | ||
} from '../../lib' | ||
import TransitionGroup from './TransitionGroup' | ||
|
||
const debug = makeDebugger('transition') | ||
|
||
const TRANSITION_TYPE = { | ||
ENTERING: 'show', | ||
EXITING: 'hide', | ||
} | ||
|
||
/** | ||
* A transition is an animation usually used to move content in or out of view. | ||
*/ | ||
|
@@ -25,7 +31,14 @@ export default class Transition extends Component { | |
children: PropTypes.element.isRequired, | ||
|
||
/** Duration of the CSS transition animation in milliseconds. */ | ||
duration: PropTypes.number, | ||
duration: PropTypes.oneOfType([ | ||
PropTypes.number, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like we technically also support a string here. |
||
PropTypes.shape({ | ||
hide: PropTypes.number, | ||
show: PropTypes.number, | ||
}), | ||
PropTypes.string, | ||
]), | ||
|
||
/** Show the component; triggers the enter or exit animation. */ | ||
visible: PropTypes.bool, | ||
|
@@ -145,7 +158,7 @@ export default class Transition extends Component { | |
this.nextStatus = null | ||
this.setState({ status, animating: true }, () => { | ||
_.invoke(this.props, 'onStart', null, { ...this.props, status }) | ||
setTimeout(this.handleComplete, duration) | ||
setTimeout(this.handleComplete, normalizeTransitionDuration(duration, 'show')) | ||
}) | ||
} | ||
|
||
|
@@ -262,9 +275,13 @@ export default class Transition extends Component { | |
|
||
computeStyle = () => { | ||
const { children, duration } = this.props | ||
const { status } = this.state | ||
|
||
const childStyle = _.get(children, 'props.style') | ||
const type = TRANSITION_TYPE[status] | ||
const animationDuration = type && `${normalizeTransitionDuration(duration, type)}ms` | ||
|
||
return { ...childStyle, animationDuration: `${duration}ms` } | ||
return { ...childStyle, animationDuration } | ||
} | ||
|
||
// ---------------------------------------- | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,14 @@ export default class TransitionGroup extends React.Component { | |
children: PropTypes.node, | ||
|
||
/** Duration of the CSS transition animation in milliseconds. */ | ||
duration: PropTypes.number, | ||
duration: PropTypes.oneOfType([ | ||
PropTypes.number, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Finally, let's add string as a supported type here too. |
||
PropTypes.shape({ | ||
hide: PropTypes.number.isRequired, | ||
show: PropTypes.number.isRequired, | ||
}), | ||
PropTypes.string, | ||
]), | ||
} | ||
|
||
static defaultProps = { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { default, TransitionProps, TRANSITION_STATUSES } from './Transition'; | ||
export { default, TransitionProps, TransitionPropDuration, TRANSITION_STATUSES } from './Transition'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Name example's class as file