diff --git a/packages/ui-toggle-details/src/ToggleDetails/README.md b/packages/ui-toggle-details/src/ToggleDetails/README.md index 212c4d418f..e3ab14c7a0 100644 --- a/packages/ui-toggle-details/src/ToggleDetails/README.md +++ b/packages/ui-toggle-details/src/ToggleDetails/README.md @@ -10,12 +10,81 @@ By default, ToggleDetails content is hidden. To override, pass in the `defaultEx --- type: example --- - - I am expanded! {lorem.paragraph()} - +const Example = () => { + const [state, setState] = useState({ + expanded: true, + fluidWidth: true, + iconPosition: 'start', + size: 'small', + variant: 'default' + }); + + const handleChange = (field, value) => setState(prevState => ({ ...prevState, [field]: value })); + const handleToggle = () => setState(prevState => ({ ...prevState, expanded: !prevState.expanded })); + + const renderOptions = () => { + const { fluidWidth, iconPosition, size, variant } = state; + const options = [ + { name: 'iconPosition', values: ['start', 'end'] }, + { name: 'size', values: ['small', 'medium', 'large'] }, + { name: 'variant', values: ['default', 'filled'] }, + ]; + + return ( + + {options.map(({ name, values }) => ( + + handleChange(name, value)} + > + {values.map(val => ( + + ))} + + + ))} + + handleChange('fluidWidth', !fluidWidth)} + /> + + + ); + }; + + const { expanded, iconPosition, size, variant, fluidWidth } = state; + + return ( +
+ {renderOptions()} + +
+
+ handleChange('expanded', expanded)} + fluidWidth={fluidWidth} + iconPosition={iconPosition} + size={size} + variant={variant} + > + + I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded! + + +
+ ); +}; + +render() ``` ToggleDetails can be controlled: diff --git a/packages/ui-toggle-details/src/ToggleDetails/index.tsx b/packages/ui-toggle-details/src/ToggleDetails/index.tsx index 13a0d667ed..c58b59b98c 100644 --- a/packages/ui-toggle-details/src/ToggleDetails/index.tsx +++ b/packages/ui-toggle-details/src/ToggleDetails/index.tsx @@ -36,7 +36,7 @@ import { testable } from '@instructure/ui-testable' import { withStyle, jsx } from '@instructure/emotion' import generateStyle from './styles' import generateComponentTheme from './theme' -import type { ToggleDetailsProps, ToggleDetailsStyleProps } from './props' +import type { ToggleDetailsProps } from './props' import { allowedProps, propTypes } from './props' import type { ExpandableToggleProps } from '@instructure/ui-expandable' @@ -75,16 +75,11 @@ class ToggleDetails extends Component { } componentDidMount() { - this.props.makeStyles?.({ animate: false } as ToggleDetailsStyleProps) + this.props.makeStyles?.() } - componentDidUpdate(prevProps: ToggleDetailsProps) { - // only animates the details when the expanded state changes (when ToggleDetails - // is used as a controlled component) - otherwise the handleToggle callback - // will animate the details - if (prevProps.expanded !== this.props.expanded) { - this.props.makeStyles?.({ animate: true } as ToggleDetailsStyleProps) - } + componentDidUpdate(_prevProps: ToggleDetailsProps) { + this.props.makeStyles?.() } getButtonRef = (el: Element | null) => (this._button = el as HTMLElement) @@ -174,7 +169,6 @@ class ToggleDetails extends Component { if (typeof this.props.onToggle === 'function') { this.props.onToggle(event, expanded) } - this.props.makeStyles?.({ animate: true }) } render() { diff --git a/packages/ui-toggle-details/src/ToggleDetails/styles.ts b/packages/ui-toggle-details/src/ToggleDetails/styles.ts index acd5b9b1e3..014ca2f9bc 100644 --- a/packages/ui-toggle-details/src/ToggleDetails/styles.ts +++ b/packages/ui-toggle-details/src/ToggleDetails/styles.ts @@ -49,10 +49,9 @@ const contentAnimation = keyframes` const generateStyle = ( componentTheme: ToggleDetailsTheme, props: ToggleDetailsProps, - state: ToggleDetailsStyleProps + _state: ToggleDetailsStyleProps ): ToggleDetailsStyle => { const { fluidWidth, iconPosition, size, variant } = props - const { animate } = state const positionIconAtEnd = iconPosition === 'end' && (variant === 'filled' || fluidWidth) @@ -178,15 +177,13 @@ const generateStyle = ( ...fontSizeStyles[size!], ...indentDetailsStyles[size!] }, - content: animate - ? { - label: 'toggleDetails__content', - opacity: 0.01, - animationName: contentAnimation, - animationFillMode: 'forwards', - animationDuration: '.3s' - } - : {} + content: { + label: 'toggleDetails__content', + opacity: 0.01, + animationName: contentAnimation, + animationFillMode: 'forwards', + animationDuration: '.3s' + } } }