Skip to content

Commit

Permalink
fix(Sticky): preserve item height (#3500)
Browse files Browse the repository at this point in the history
* Preserve sticky item height fixes(3499)

* Add above content example

* Update StickyExampleAboveContent.js
  • Loading branch information
felixmosh authored and layershifter committed Mar 20, 2019
1 parent 9aff7ea commit 6eefe00
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
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

0 comments on commit 6eefe00

Please sign in to comment.