Skip to content
This repository was archived by the owner on Feb 16, 2023. It is now read-only.

Commit add719b

Browse files
author
richardadjogah
committed
Enable align feature via a prop, add position fix for ‘start’ alignment
1 parent 41998f2 commit add719b

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

lib/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ const Popover = createClass({
6666
isOpen: T.bool,
6767
onOuterAction: T.func,
6868
enterExitTransitionDurationMs: T.number,
69+
align: T.string,
6970
},
7071
getInitialState () {
7172
return {
@@ -86,6 +87,7 @@ const Popover = createClass({
8687
enterExitTransitionDurationMs: 500,
8788
target: null,
8889
refreshIntervalMs: 200,
90+
align: `center`,
8991
}
9092
},
9193
checkTargetReposition () {
@@ -138,7 +140,7 @@ const Popover = createClass({
138140
/* When positioning self on the cross-axis do not exceed frame bounds. The strategy to achieve
139141
this is thus: First position cross-axis self to the cross-axis-center of the the target. Then,
140142
offset self by the amount that self is past the boundaries of frame. */
141-
const pos = Layout.calcRelPos(zone, tb, this.size)
143+
const pos = Layout.calcRelPos(zone, tb, this.size, this.props.align)
142144

143145
/* Offset allows users to control the distance betweent the tip and the target. */
144146
pos[axis.main.start] += this.props.offset * zone.order

lib/layout.js

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,30 @@ const centerOfBoundsFromBounds = (flow, axis, boundsTo, boundsFrom) => (
5656

5757
const place = (flow, axis, align, bounds, size) => {
5858
const axisProps = axes[flow][axis]
59-
return (
60-
align === `center`
61-
? centerOfBounds(flow, axis, bounds) - centerOfSize(flow, axis, size)
62-
: align === `end`
63-
? bounds[axisProps.end]
64-
: align === `start`
65-
/* DOM rendering unfolds leftward. Therefore if the slave is positioned before
66-
the master then the slave`s position must in addition be pulled back
67-
by its [the slave`s] own length. */
68-
? bounds[axisProps.start] - size[axisProps.size]
69-
: null
70-
)
59+
if (axis === `main`) {
60+
return (
61+
align === `center`
62+
? centerOfBounds(flow, axis, bounds) - centerOfSize(flow, axis, size)
63+
: align === `end`
64+
? bounds[axisProps.end]
65+
: align === `start`
66+
/* DOM rendering unfolds leftward. Therefore if the slave is positioned before
67+
the master then the slave`s position must in addition be pulled back
68+
by its [the slave`s] own length. */
69+
? bounds[axisProps.start] - size[axisProps.size]
70+
: null
71+
)
72+
} else {
73+
return (
74+
align === `center`
75+
? centerOfBounds(flow, axis, bounds) - centerOfSize(flow, axis, size)
76+
: align === `end`
77+
? bounds[axisProps.end]
78+
: align === `start`
79+
? bounds[axisProps.start]
80+
: null
81+
)
82+
}
7183
}
7284

7385

@@ -208,13 +220,12 @@ const pickZone = (opts, frameBounds, targetBounds, size) => {
208220

209221
/* TODO Document this. */
210222

211-
const calcRelPos = (zone, masterBounds, slaveSize) => {
223+
const calcRelPos = (zone, masterBounds, slaveSize, align) => {
212224
const { main, cross } = axes[zone.flow]
213225
/* TODO: The slave is hard-coded to align cross-center with master. */
214-
const crossAlign = `center`
215226
const mainStart = place(zone.flow, `main`, zone.side, masterBounds, slaveSize)
216227
const mainSize = slaveSize[main.size]
217-
const crossStart = place(zone.flow, `cross`, crossAlign, masterBounds, slaveSize)
228+
const crossStart = place(zone.flow, `cross`, align, masterBounds, slaveSize)
218229
const crossSize = slaveSize[cross.size]
219230

220231
return {

0 commit comments

Comments
 (0)