-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix flickering when expanding/collapsing sections (#595)
* fix: fix flickering when expanding/collapsing sections
- Loading branch information
Showing
16 changed files
with
321 additions
and
330 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,81 @@ | ||
'use strict'; | ||
|
||
import React, {Component} from 'react'; | ||
import React, {useContext, useLayoutEffect, useState} from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import classNames from 'classnames'; | ||
import {isEmpty, isFunction} from 'lodash'; | ||
import {Card, Disclosure} from '@gravity-ui/uikit'; | ||
import {Disclosure} from '@gravity-ui/uikit'; | ||
import {MeasurementContext} from './measurement-context'; | ||
|
||
export default class Details extends Component { | ||
static propTypes = { | ||
type: PropTypes.oneOf(['text', 'image']), | ||
title: PropTypes.oneOfType([PropTypes.element, PropTypes.string]).isRequired, | ||
content: PropTypes.oneOfType([PropTypes.func, PropTypes.string, PropTypes.element, PropTypes.array]).isRequired, | ||
extendClassNames: PropTypes.oneOfType([PropTypes.array, PropTypes.string]), | ||
onClick: PropTypes.func, | ||
asHtml: PropTypes.bool | ||
}; | ||
|
||
state = {isOpened: false}; | ||
|
||
handleClick = () => { | ||
this.setState((state, props) => { | ||
const newState = {isOpened: !state.isOpened}; | ||
export default function Details(props) { | ||
const [isOpened, setIsOpened] = useState(false); | ||
|
||
if (props.onClick) { | ||
props.onClick(newState); | ||
} | ||
|
||
return newState; | ||
}); | ||
}; | ||
const handleClick = () => { | ||
const newIsOpened = !isOpened; | ||
|
||
stopPropagation = (e) => { | ||
e.stopPropagation(); | ||
setIsOpened(newIsOpened); | ||
props.onClick?.({isOpened: newIsOpened}); | ||
}; | ||
|
||
_getContent() { | ||
const content = this.props.content; | ||
const getContent = () => { | ||
const {content} = props; | ||
|
||
return isFunction(content) ? content() : content; | ||
} | ||
}; | ||
|
||
_renderContent() { | ||
if (!this.state.isOpened) { | ||
const renderContent = () => { | ||
if (!isOpened) { | ||
return null; | ||
} | ||
|
||
const children = this.props.asHtml ? null : this._getContent(); | ||
const extraProps = this.props.asHtml ? {dangerouslySetInnerHTML: {__html: this._getContent()}} : {}; | ||
const children = props.asHtml ? null : getContent(); | ||
const extraProps = props.asHtml ? {dangerouslySetInnerHTML: {__html: getContent()}} : {}; | ||
|
||
return <div className='details__content' {...extraProps}> | ||
{children} | ||
</div>; | ||
} | ||
}; | ||
|
||
render() { | ||
const {type, title, content, extendClassNames} = this.props; | ||
const className = classNames( | ||
'details', | ||
extendClassNames | ||
); | ||
const {title, content, extendClassNames} = props; | ||
const className = classNames( | ||
'details', | ||
extendClassNames | ||
); | ||
|
||
return ( | ||
isEmpty(content) && !isFunction(content) ? ( | ||
<div className={className}> | ||
{title} | ||
</div> | ||
) : ( | ||
<Disclosure className={className} onUpdate={this.handleClick} | ||
size='l'> | ||
<Disclosure.Summary> | ||
{(props, defaultButton) => ( | ||
<div className={classNames(className, 'details__summary')} aria-controls={props.ariaControls} onClick={props.onClick} id={props.id}> | ||
<div className="details__expand-button" onClick={this.stopPropagation}> | ||
{defaultButton} | ||
</div> | ||
{title} | ||
const {measure} = useContext(MeasurementContext); | ||
useLayoutEffect(() => { | ||
measure?.(); | ||
}, [isOpened]); | ||
|
||
return ( | ||
isEmpty(content) && !isFunction(content) ? ( | ||
<div className={className}> | ||
{title} | ||
</div> | ||
) : ( | ||
<Disclosure className={className} onUpdate={handleClick} size='l' expanded={isOpened}> | ||
<Disclosure.Summary> | ||
{(props, defaultButton) => ( | ||
<div className={classNames(className, 'details__summary')} aria-controls={props.ariaControls} onClick={props.onClick} id={props.id}> | ||
<div className="details__expand-button" onClick={e => e.stopPropagation()}> | ||
{defaultButton} | ||
</div> | ||
)} | ||
</Disclosure.Summary> | ||
{type === 'image' ? this._renderContent() : | ||
<Card className='details__card' view='filled'> | ||
{this._renderContent()} | ||
</Card>} | ||
</Disclosure> | ||
) | ||
); | ||
} | ||
{title} | ||
</div> | ||
)} | ||
</Disclosure.Summary> | ||
{renderContent()} | ||
</Disclosure> | ||
) | ||
); | ||
} | ||
|
||
Details.propTypes = { | ||
id: PropTypes.string, | ||
ariaControls: PropTypes.arrayOf(PropTypes.string), | ||
title: PropTypes.oneOfType([PropTypes.element, PropTypes.string]).isRequired, | ||
content: PropTypes.oneOfType([PropTypes.func, PropTypes.string, PropTypes.element, PropTypes.array]).isRequired, | ||
extendClassNames: PropTypes.oneOfType([PropTypes.array, PropTypes.string]), | ||
onClick: PropTypes.func, | ||
asHtml: PropTypes.bool | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import {createContext} from 'react'; | ||
|
||
export const MeasurementContext = createContext({measure: () => {}}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.