Skip to content

Commit

Permalink
feat(Visibility): add direction for calculations (#2090)
Browse files Browse the repository at this point in the history
  • Loading branch information
layershifter authored and levithomason committed Sep 23, 2017
1 parent 5fbba00 commit 8b1b02d
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Any other issue labeled [`help wanted`][4] is ready for a PR.
|-----------------|-----------------|-----------------|-----------------|--------------------|
| ✓ Button | ✓ Breadcrumb | ✓ Advertisement | ✓ Accordion | Form Validation |
| ✓ Container | ✓ Form | ✓ Card | ✓ Checkbox | *API (NA)* |
| ✓ Divider | ✓ Grid | ✓ Comment | ✓ Dimmer | ✓ Visibility (NA) |
| ✓ Divider | ✓ Grid | ✓ Comment | ✓ Dimmer | ✓ Visibility |
| ✓ Flag | ✓ Menu | ✓ Feed | ✓ Dropdown | |
| ✓ Header | ✓ Message | ✓ Item | ✓ Embed | |
| ✓ Icon | ✓ Table | ✓ Statistic | ✓ Modal | |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Divider, Grid, Image, Table, Segment, Visibility } from 'semantic-ui-re
export default class VisibilityExampleFireOnMount extends Component {
state = {
calculations: {
direction: 'none',
height: 0,
width: 0,
topPassed: false,
Expand Down Expand Up @@ -55,6 +56,10 @@ export default class VisibilityExampleFireOnMount extends Component {
</Table.Row>
</Table.Header>
<Table.Body>
<Table.Row>
<Table.Cell>direction</Table.Cell>
<Table.Cell>{calculations.direction}</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>pixelsPassed</Table.Cell>
<Table.Cell>{calculations.pixelsPassed.toFixed()}px</Table.Cell>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Wireframe from '../Wireframe'
export default class VisibilityExampleVisibility extends Component {
state = {
calculations: {
direction: 'none',
height: 0,
width: 0,
topPassed: false,
Expand Down Expand Up @@ -47,6 +48,10 @@ export default class VisibilityExampleVisibility extends Component {
</Table.Row>
</Table.Header>
<Table.Body>
<Table.Row>
<Table.Cell>direction</Table.Cell>
<Table.Cell>{calculations.direction}</Table.Cell>
</Table.Row>
<Table.Row>
<Table.Cell>pixelsPassed</Table.Cell>
<Table.Cell>{calculations.pixelsPassed.toFixed()}px</Table.Cell>
Expand Down
1 change: 1 addition & 0 deletions src/behaviors/Visibility/Visibility.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export interface VisibilityProps {
export interface VisibilityCalculations {
bottomPassed: boolean;
bottomVisible: boolean;
direction: 'down' | 'up';
fits: boolean;
height: number;
passing: boolean;
Expand Down
5 changes: 5 additions & 0 deletions src/behaviors/Visibility/Visibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,9 @@ export default class Visibility extends Component {

const { context, fireOnMount } = this.props

this.pageYOffset = window.pageYOffset
context.addEventListener('scroll', this.handleScroll)

if (fireOnMount) this.update()
}

Expand Down Expand Up @@ -272,6 +274,7 @@ export default class Visibility extends Component {

this.oldCalculations = this.calculations
this.calculations = this.computeCalculations()
this.pageYOffset = window.pageYOffset

const {
onBottomPassed,
Expand Down Expand Up @@ -322,6 +325,7 @@ export default class Visibility extends Component {
const { bottom, height, top, width } = this.ref.getBoundingClientRect()
const [topOffset, bottomOffset] = normalizeOffset(offset)

const direction = window.pageYOffset > this.pageYOffset ? 'down' : 'up'
const topPassed = top < topOffset
const bottomPassed = bottom < bottomOffset

Expand All @@ -340,6 +344,7 @@ export default class Visibility extends Component {
return {
bottomPassed,
bottomVisible,
direction,
fits,
height,
passing,
Expand Down
35 changes: 35 additions & 0 deletions test/specs/behaviors/Visibility/Visibility-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,41 @@ describe('Visibility', () => {
})
})
})

describe('direction', () => {
let pageYOffset

beforeEach(() => {
pageYOffset = window.pageYOffset
})

afterEach(() => {
window.pageYOffset = pageYOffset
})

it('returns up when scrolling down', () => {
const onUpdate = sandbox.spy()
mount(<Visibility onUpdate={onUpdate} />)

window.pageYOffset = 5
domEvent.scroll(window)
onUpdate.should.have.been.calledWithMatch(null, {
calculations: { direction: 'down' },
})
})

it('returns up when scrolling up', () => {
window.pageYOffset = 100
const onUpdate = sandbox.spy()
mount(<Visibility onUpdate={onUpdate} />)

window.pageYOffset = 50
domEvent.scroll(window)
onUpdate.should.have.been.calledWithMatch(null, {
calculations: { direction: 'up' },
})
})
})
})

describe('context', () => {
Expand Down

0 comments on commit 8b1b02d

Please sign in to comment.