Skip to content

Commit

Permalink
feat(Visibility): add fireOnMount (#2018)
Browse files Browse the repository at this point in the history
* feat(Visibility): add fireOnMount

* docs(Visibility): remove "once" from fireOnMount
  • Loading branch information
layershifter authored and levithomason committed Sep 1, 2017
1 parent ef9faee commit 578b957
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import React, { Component } from 'react'
import { Divider, Grid, Image, Table, Segment, Visibility } from 'semantic-ui-react'

export default class VisibilityExampleFireOnMount extends Component {
state = {
calculations: {
height: 0,
width: 0,
topPassed: false,
bottomPassed: false,
pixelsPassed: 0,
percentagePassed: 0,
topVisible: false,
bottomVisible: false,
fits: false,
passing: false,
onScreen: false,
offScreen: false,
},
}

handleOnScreen = (e, { calculations }) => this.setState({ calculations })

handleOffScreen = (e, { calculations }) => this.setState({ calculations })

render() {
const { calculations } = this.state

return (
<Grid columns={2}>
<Grid.Column>
<Visibility
fireOnMount
onOnScreen={this.handleOnScreen}
onOffScreen={this.handleOffScreen}
>
<Segment>
<Image src='/assets/images/wireframe/centered-paragraph.png' />
<Divider />
<Image src='/assets/images/wireframe/short-paragraph.png' />
<Divider />
<Image src='/assets/images/wireframe/media-paragraph.png' />
<Divider />
<Image src='/assets/images/wireframe/paragraph.png' />
</Segment>
</Visibility>
</Grid.Column>

<Grid.Column>
<Table celled>
<Table.Header>
<Table.Row>
<Table.HeaderCell>Calculation</Table.HeaderCell>
<Table.HeaderCell>Value</Table.HeaderCell>
</Table.Row>
</Table.Header>
<Table.Body>
<Table.Row>
<Table.Cell>pixelsPassed</Table.Cell>
<Table.Cell>{calculations.pixelsPassed.toFixed()}px</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>percentagePassed</Table.Cell>
<Table.Cell>{(calculations.percentagePassed * 100).toFixed()}%</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>fits</Table.Cell>
<Table.Cell>{calculations.fits.toString()}</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>width</Table.Cell>
<Table.Cell>{calculations.width.toFixed()}px</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>height</Table.Cell>
<Table.Cell>{calculations.height.toFixed()}px</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>onScreen</Table.Cell>
<Table.Cell>{calculations.onScreen.toString()}</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>offScreen</Table.Cell>
<Table.Cell>{calculations.offScreen.toString()}</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>passing</Table.Cell>
<Table.Cell>{calculations.passing.toString()}</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>topVisible</Table.Cell>
<Table.Cell>{calculations.topVisible.toString()}</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>bottomVisible</Table.Cell>
<Table.Cell>{calculations.bottomVisible.toString()}</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>topPassed</Table.Cell>
<Table.Cell>{calculations.topPassed.toString()}</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>bottomPassed</Table.Cell>
<Table.Cell>{calculations.bottomPassed.toString()}</Table.Cell>
</Table.Row>
</Table.Body>
</Table>
</Grid.Column>
</Grid>
)
}
}
9 changes: 7 additions & 2 deletions docs/app/Examples/behaviors/Visibility/Settings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ import ExampleSection from 'docs/app/Components/ComponentDoc/ExampleSection'

const VisibilityExample = () => (
<ExampleSection title='Settings'>
<ComponentExample
title='Fire on mount'
description='Fires callbacks immediately after mount.'
examplePath='behaviors/Visibility/Settings/VisibilityExampleFireOnMount'
/>
<ComponentExample
title='Callback frequency'
description='You can change the callback frequency with `once` and `continuous` settings'
description='You can change the callback frequency with `once` and `continuous` settings.'
examplePath='behaviors/Visibility/Settings/CallbackFrequencyExample'
/>
<ComponentExample
title='Grouped callbacks'
description='You can specify callbacks that occur after different percentages or pixels of an element are passed'
description='You can specify callbacks that occur after different percentages or pixels of an element are passed.'
examplePath='behaviors/Visibility/Settings/GroupedCallbacksExample'
/>
</ExampleSection>
Expand Down
2 changes: 1 addition & 1 deletion docs/app/Examples/behaviors/Visibility/Types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const VisibilityExample = () => (
<ExampleSection title='Types'>
<ComponentExample
title='Default'
description='An example of Visibility'
description='An example of Visibility.'
examplePath='behaviors/Visibility/Types/VisibilityExample'
/>
</ExampleSection>
Expand Down
3 changes: 3 additions & 0 deletions src/behaviors/Visibility/Visibility.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export interface VisibilityProps {
*/
continuous?: boolean;

/** Fires callbacks immediately after mount. */
fireOnMount?: boolean;

/**
* Element's bottom edge has passed top of screen.
*
Expand Down
13 changes: 9 additions & 4 deletions src/behaviors/Visibility/Visibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ export default class Visibility extends Component {
*/
continuous: PropTypes.bool,

/** Fires callbacks immediately after mount. */
fireOnMount: PropTypes.bool,

/**
* Element's bottom edge has passed top of screen.
*
Expand Down Expand Up @@ -173,15 +176,17 @@ export default class Visibility extends Component {
componentDidMount() {
if (!isBrowser) return

const { context } = this.props
context.addEventListener('scroll', this.handleScroll)
const { context, fireOnMount } = this.props

context.addEventListener('scroll', this.handleUpdate)
if (fireOnMount) this.handleUpdate()
}

componentWillUnmount() {
if (!isBrowser) return

const { context } = this.props
context.removeEventListener('scroll', this.handleScroll)
context.removeEventListener('scroll', this.handleUpdate)
}

execute = (callback, name) => {
Expand Down Expand Up @@ -274,7 +279,7 @@ export default class Visibility extends Component {

handleRef = c => (this.ref = c)

handleScroll = () => {
handleUpdate = () => {
const { bottom, height, top, width } = this.ref.getBoundingClientRect()

const topPassed = top < 0
Expand Down
15 changes: 15 additions & 0 deletions test/specs/behaviors/Visibility/Visibility-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,21 @@ describe('Visibility', () => {
})
})

describe('fireOnMount', () => {
it('fires callbacks after mount', () => {
const onUpdate = sandbox.spy()

mockScroll(0, 0)
wrapperMount(<Visibility fireOnMount onUpdate={onUpdate} />)

onUpdate.should.have.been.calledOnce()
onUpdate.should.have.been.calledWithMatch(null, {
calculations: { height: 0, width: 0 },
fireOnMount: true,
})
})
})

describe('onPassed', () => {
it('fires callback when pixels passed', () => {
const onPassed = {
Expand Down

0 comments on commit 578b957

Please sign in to comment.