Skip to content

Commit

Permalink
breaking(popup): verticalOffset (Semantic-Org#2450)
Browse files Browse the repository at this point in the history
* feat(popup): verticalOffset

Add a verticalOffset prop to the popup.

* feat(popup): Rename offset prop

Rename `offset` to `horizontalOffset` for consistency.

* docs(popup): Update for offset

Update docs for `horizontalOffset` and the new `verticalOffset` property.

* fix(popup): vertical offset bug

Stop popup disappearing when vertical offset applied.
  • Loading branch information
adam-26 authored and Brandon Lawrence committed Mar 14, 2018
1 parent 79b2ec0 commit ed2386b
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 13 deletions.
16 changes: 14 additions & 2 deletions docs/app/Examples/modules/Popup/Variations/PopupExampleOffset.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,27 @@ const PopupExampleOffset = () => (
<Popup
trigger={<Icon size='large' name='heart' circular />}
content='Way off to the left'
offset={50}
horizontalOffset={50}
position='left center'
/>
<Popup
trigger={<Icon size='large' name='heart' circular />}
content='As expected this popup is way off to the right'
offset={50}
horizontalOffset={50}
position='right center'
/>
<Popup
trigger={<Icon size='large' name='heart' circular />}
content='Way off to the top'
verticalOffset={50}
position='top center'
/>
<Popup
trigger={<Icon size='large' name='heart' circular />}
content='As expected this popup is way off to the bottom'
verticalOffset={50}
position='bottom center'
/>
</div>
)

Expand Down
2 changes: 1 addition & 1 deletion docs/app/Examples/modules/Popup/Variations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const PopupVariationsExamples = () => (
/>
<ComponentExample
title='Offset'
description='A popup position can be adjusted manually by specifying an offset property.'
description='A popup position can be offset from its position.'
examplePath='modules/Popup/Variations/PopupExampleOffset'
/>
<ComponentExample
Expand Down
5 changes: 4 additions & 1 deletion src/modules/Popup/Popup.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ export interface PopupProps extends PortalProps {
inverted?: boolean;

/** Horizontal offset in pixels to be applied to the popup. */
offset?: number;
horizontalOffset?: number;

/** Vertical offset in pixels to be applied to the popup. */
verticalOffset?: number;

/** Events triggering the popup. */
on?: 'hover' | 'click' | 'focus' | Array<'hover' | 'click' | 'focus'>;
Expand Down
21 changes: 16 additions & 5 deletions src/modules/Popup/Popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ export default class Popup extends Component {
inverted: PropTypes.bool,

/** Horizontal offset in pixels to be applied to the Popup. */
offset: PropTypes.number,
horizontalOffset: PropTypes.number,

/** Vertical offset in pixels to be applied to the Popup. */
verticalOffset: PropTypes.number,

/** Events triggering the popup. */
on: PropTypes.oneOfType([
Expand Down Expand Up @@ -153,7 +156,7 @@ export default class Popup extends Component {
// Do not access window/document when server side rendering
if (!isBrowser()) return style

const { offset } = this.props
const { horizontalOffset, verticalOffset } = this.props
const { pageYOffset, pageXOffset } = window
const { clientWidth, clientHeight } = document.documentElement

Expand Down Expand Up @@ -188,11 +191,19 @@ export default class Popup extends Component {
}
}

if (offset) {
if (horizontalOffset) {
if (_.isNumber(style.right)) {
style.right -= offset
style.right -= horizontalOffset
} else {
style.left -= horizontalOffset
}
}

if (verticalOffset) {
if (_.isNumber(style.top)) {
style.top += verticalOffset
} else {
style.left -= offset
style.bottom += verticalOffset
}
}

Expand Down
53 changes: 49 additions & 4 deletions test/specs/modules/Popup/Popup-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe('Popup', () => {
it('accepts an offest to the left', () => {
wrapperMount(
<Popup
offset={50}
horizontalOffset={50}
position='bottom right'
content='foo'
trigger={<button>foo</button>}
Expand All @@ -96,7 +96,7 @@ describe('Popup', () => {
it('accepts an offest to the right', () => {
wrapperMount(
<Popup
offset={50}
horizontalOffset={50}
position='bottom left'
content='foo'
trigger={<button>foo</button>}
Expand All @@ -108,6 +108,35 @@ describe('Popup', () => {
})
})

describe('verticalOffest', () => {
it('accepts a vertical offest to the top', () => {
wrapperMount(
<Popup
verticalOffset={50}
position='bottom right'
content='foo'
trigger={<button>foo</button>}
/>,
)

wrapper.find('button').simulate('click')
assertInBody('.ui.popup.visible')
})
it('accepts a vertical offest to the bottom', () => {
wrapperMount(
<Popup
verticalOffset={50}
position='top left'
content='foo'
trigger={<button>foo</button>}
/>,
)

wrapper.find('button').simulate('click')
assertInBody('.ui.popup.visible')
})
})

describe('position', () => {
POSITIONS.forEach((position) => {
it('is always within the viewport', () => {
Expand All @@ -129,14 +158,30 @@ describe('Popup', () => {
expect(bottom).to.be.at.most(document.documentElement.clientHeight)
expect(right).to.be.at.most(document.documentElement.clientWidth)
})
it('is the original if no position fits within the viewport', () => {
it('is the original if no horizontal position fits within the viewport', () => {
wrapperMount(
<Popup
content='_'
position={position}
trigger={<button>foo</button>}
on='click'
horizontalOffset={999}
/>,
)
wrapper.find('button').simulate('click')
const selectedPosition = wrapper.state('position')

expect(selectedPosition).to.equal(position)
})

it('is the original if no vertical position fits within the viewport', () => {
wrapperMount(
<Popup
content='_'
position={position}
trigger={<button>foo</button>}
on='click'
offset={999}
verticalOffset={3000}
/>,
)
wrapper.find('button').simulate('click')
Expand Down

0 comments on commit ed2386b

Please sign in to comment.