* Converts a string containing value + unit into a px value number * @function * @memberof {modifiers~offset} * @private * @argument {String} str - Value + unit string * @argument {String} measurement - `height` or `width` * @argument {Object} popperOffsets * @argument {Object}
(str, measurement, popperOffsets, referenceOffsets)
| 3175 | * Value in pixels, or original string if no values were extracted |
| 3176 | */ |
| 3177 | function toValue(str, measurement, popperOffsets, referenceOffsets) { |
| 3178 | // separate value from unit |
| 3179 | var split = str.match(/((?:\-|\+)?\d*\.?\d*)(.*)/); |
| 3180 | var value = +split[1]; |
| 3181 | var unit = split[2]; |
| 3182 | |
| 3183 | // If it's not a number it's an operator, I guess |
| 3184 | if (!value) { |
| 3185 | return str; |
| 3186 | } |
| 3187 | |
| 3188 | if (unit.indexOf('%') === 0) { |
| 3189 | var element = void 0; |
| 3190 | switch (unit) { |
| 3191 | case '%p': |
| 3192 | element = popperOffsets; |
| 3193 | break; |
| 3194 | case '%': |
| 3195 | case '%r': |
| 3196 | default: |
| 3197 | element = referenceOffsets; |
| 3198 | } |
| 3199 | |
| 3200 | var rect = getClientRect(element); |
| 3201 | return rect[measurement] / 100 * value; |
| 3202 | } else if (unit === 'vh' || unit === 'vw') { |
| 3203 | // if is a vh or vw, we calculate the size based on the viewport |
| 3204 | var size = void 0; |
| 3205 | if (unit === 'vh') { |
| 3206 | size = Math.max(document.documentElement.clientHeight, window.innerHeight || 0); |
| 3207 | } else { |
| 3208 | size = Math.max(document.documentElement.clientWidth, window.innerWidth || 0); |
| 3209 | } |
| 3210 | return size / 100 * value; |
| 3211 | } else { |
| 3212 | // if is an explicit pixel unit, we get rid of the unit and keep the value |
| 3213 | // if is an implicit unit, it's px, and we return just the value |
| 3214 | return value; |
| 3215 | } |
| 3216 | } |
| 3217 | |
| 3218 | /** |
| 3219 | * Parse an `offset` string to extrapolate `x` and `y` numeric offsets. |
no test coverage detected