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

fix(Sticky): preserve item height #3500

Merged
merged 4 commits into from
Mar 20, 2019
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React, { Component, createRef } from 'react'
import { Image, Input, Menu, Segment, Sticky } from 'semantic-ui-react'
import _ from 'lodash'

export default class StickyExampleAboveContent extends Component {
contextRef = createRef()

render() {
return (
<div ref={this.contextRef}>
<Sticky context={this.contextRef}>
<Menu
attached='top'
tabular
style={{ backgroundColor: '#fff', paddingTop: '1em' }}
>
<Menu.Item as='a' active name='bio' />
<Menu.Item as='a' active={false} name='photos' />
<Menu.Menu position='right'>
<Menu.Item>
<Input
transparent
icon={{ name: 'search', link: true }}
placeholder='Search users...'
/>
</Menu.Item>
</Menu.Menu>
</Menu>
</Sticky>
<Segment attached='bottom'>
{_.times(5, i => <Image key={i} src='/images/wireframe/paragraph.png' />)}
</Segment>
</div>
)
}
}
5 changes: 5 additions & 0 deletions docs/src/examples/modules/Sticky/Usage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ const StickyUsageExamples = () => (
description='Define the offset from the top/bottom of the screen when fixing element to viewport.'
examplePath='modules/Sticky/Usage/StickyExampleOffset'
/>
<ComponentExample
title='Above Content'
description='Sticky content that is above other content'
examplePath='modules/Sticky/Usage/StickyExampleAboveContent'
/>
</ExampleSection>
)

Expand Down
5 changes: 3 additions & 2 deletions src/modules/Sticky/Sticky.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import PropTypes from 'prop-types'
import React, { Component, createRef } from 'react'

import {
eventStack,
customPropTypes,
eventStack,
getElementType,
getUnhandledProps,
isBrowser,
Expand Down Expand Up @@ -316,10 +316,11 @@ export default class Sticky extends Component {
sticky && !bound && (bottom === null ? 'top' : 'bottom'),
'sticky',
)
const triggerStyles = sticky && this.stickyRect ? { height: this.stickyRect.height } : {}

return (
<ElementType {...rest} className={containerClasses}>
<div ref={this.triggerRef} />
<div ref={this.triggerRef} style={triggerStyles} />
<div className={elementClasses} ref={this.stickyRef} style={this.computeStyle()}>
{children}
</div>
Expand Down
14 changes: 14 additions & 0 deletions test/specs/modules/Sticky/Sticky-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,20 @@ describe('Sticky', () => {
.childAt(1)
.should.have.style('bottom', '0px')
})

it('should preserve sticky element height', () => {
mockContextEl()
mockPositions({ bottomOffset: 0, height: 100, offset: 0 })
wrapperMount(<Sticky {...positions} context={contextEl} />)

// Scroll after trigger
scrollAfterTrigger()

wrapper
.childAt(0)
.childAt(0)
.should.have.style('height', '100px')
})
})
describe('onBottom', () => {
it('is called with (e, data) when is on bottom', () => {
Expand Down