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(Popup): add vertical offset #1146

Closed
wants to merge 3 commits into from
Closed
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 src/collections/Message/Message.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { default as MessageList } from './MessageList';
export interface MessageProps {
[key: string]: any;

/** An element type to render as (string or function). */
/** An element type to render as (string or function). */
Copy link
Member

Choose a reason for hiding this comment

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

Lint fix. Quite strange issue.

as?: any;

/** A message can be formatted to attach itself to other content. */
Expand Down
2 changes: 1 addition & 1 deletion src/modules/Popup/Popup.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export interface PopupProps extends PortalProps {
inverted?: boolean;

/** Horizontal offset in pixels to be applied to the popup. */
offset?: number;
offset?: number | Array<number>;

/** Event triggering the popup. */
on?: 'hover' | 'click' | 'focus';
Expand Down
37 changes: 26 additions & 11 deletions src/modules/Popup/Popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ export default class Popup extends Component {
inverted: PropTypes.bool,

/** Horizontal offset in pixels to be applied to the Popup. */
offset: PropTypes.number,
offset: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.number),
PropTypes.number,
]),

/** Event triggering the popup. */
on: PropTypes.oneOf(['hover', 'click', 'focus']),
Expand Down Expand Up @@ -126,6 +129,7 @@ export default class Popup extends Component {

static defaultProps = {
position: 'top left',
offset: 0,
on: 'hover',
}

Expand All @@ -139,13 +143,32 @@ export default class Popup extends Component {

state = {}

computeHorizontalOffset = ({ left, right }, offset) => {
if (_.isNumber(right)) return { left, right: right - offset }
return { right, left: left - offset }
}

computeVerticalOffset = ({ bottom, top }, offset) => {
if (_.isNumber(top)) return { bottom, top: top + offset }
return { top, bottom: bottom + offset }
}

computeOffset = style => {
const { offset } = this.props
const [horizontal, vertical] = _.isNumber(offset) ? [offset, 0] : offset

return {
...this.computeHorizontalOffset(style, horizontal),
...this.computeVerticalOffset(style, vertical),
}
}

computePopupStyle(positions) {
const style = { position: 'absolute' }

// Do not access window/document when server side rendering
if (!isBrowser) return style

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

Expand Down Expand Up @@ -180,15 +203,7 @@ export default class Popup extends Component {
}
}

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

return style
return { ...style, ...this.computeOffset(style) }
}

// check if the style would display
Expand Down
11 changes: 6 additions & 5 deletions test/specs/modules/Popup/Popup-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ describe('Popup', () => {
assertInBody('.ui.popup.visible.some-class')
})

describe('offest', () => {
it('accepts an offest to the left', () => {
describe('offset', () => {
it('accepts an offset as number', () => {
wrapperMount(
<Popup
offset={50}
Expand All @@ -92,11 +92,12 @@ describe('Popup', () => {
wrapper.find('button').simulate('click')
assertInBody('.ui.popup.visible')
})
it('accepts an offest to the right', () => {

it('accepts an offset as array', () => {
wrapperMount(
<Popup
offset={50}
position='bottom left'
offset={[50, 50]}
position='bottom right'
content='foo'
trigger={<button>foo</button>}
/>
Expand Down