Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Visibility): add direction for calculations #2090

Merged
merged 2 commits into from
Sep 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) |
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cruft 😄

| ✓ 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