Skip to content

Commit

Permalink
fix(panel): recalc body height when expanding menus
Browse files Browse the repository at this point in the history
  • Loading branch information
crimx committed May 28, 2018
1 parent c93c7b9 commit b6a5714
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/components/dictionaries/cambridge/View.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ import React from 'react'
import Speaker from '@/components/Speaker'
import { CambridgeResult } from './engine'

export default class DictCambridge extends React.PureComponent<{ result: CambridgeResult }> {
export interface DictCambridgeProps {
result: CambridgeResult
recalcBodyHeight: () => any
}

export default class DictCambridge extends React.PureComponent<DictCambridgeProps> {
handleEntryClick = (e: React.MouseEvent<HTMLElement>) => {
const target = e.nativeEvent.target as HTMLDivElement
if (target.classList && target.classList.contains('js-accord')) {
target.classList.toggle('open')
this.props.recalcBodyHeight()
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/components/dictionaries/oald/View.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ import React from 'react'
import Speaker from '@/components/Speaker'
import { OALDResult, OALDResultLex, OALDResultRelated } from './engine'

export default class DictOALD extends React.PureComponent<{ result: OALDResult }> {
export interface DictOALDProps {
result: OALDResult
recalcBodyHeight: () => any
}

export default class DictOALD extends React.PureComponent<DictOALDProps> {
handleEntryClick = (e: React.MouseEvent<HTMLElement>) => {
const target = e.nativeEvent.target as HTMLSpanElement
if (target.classList && target.classList.contains('heading')) {
(target.parentElement as HTMLDivElement).classList.toggle('is-active')
this.props.recalcBodyHeight()
}
}

Expand Down
22 changes: 21 additions & 1 deletion src/content/components/DictItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,20 @@ export class DictItem extends React.PureComponent<DictItemProps & { t: Translati
})
}

handleRecalcBodyHeight = () => {
setTimeout(() => {
if (this.bodyRef.current) {
const offsetHeight = Math.max(this.bodyRef.current.offsetHeight, 10) || 10
this.setState({
offsetHeight,
visibleHeight: offsetHeight,
isUnfold: true,
hasError: false,
})
}
}, 0)
}

componentDidUpdate (prevProps: DictItemProps) {
if (this.props.searchStatus === SearchStatus.Finished &&
this.props.searchStatus !== prevProps.searchStatus) {
Expand Down Expand Up @@ -246,7 +260,13 @@ export class DictItem extends React.PureComponent<DictItemProps & { t: Translati
>
<article ref={this.bodyRef} className='panel-DictItem_BodyMesure' style={{ opacity }}>
{searchResult && !hasError &&
React.createElement(require('@/components/dictionaries/' + id + '/View.tsx').default, { result: searchResult })
React.createElement(
require('@/components/dictionaries/' + id + '/View.tsx').default,
{
result: searchResult,
recalcBodyHeight: this.handleRecalcBodyHeight,
}
)
}
</article>
{isUnfold && searchResult && visibleHeight < offsetHeight &&
Expand Down

0 comments on commit b6a5714

Please sign in to comment.