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

Add support for rem values in gap and peek #599

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/components/gaps.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { toInt } from '../utils/unit'
import { toPx } from '../utils/unit'
import { define } from '../utils/object'
import { throttle } from '../utils/wait'

Expand Down Expand Up @@ -58,7 +58,7 @@ export default function (Glide, Components, Events) {
* @returns {Number}
*/
get () {
return toInt(Glide.settings.gap)
return toPx(Glide.settings.gap)
}
})

Expand Down
8 changes: 4 additions & 4 deletions src/components/peek.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { define } from '../utils/object'
import { toInt, isObject } from '../utils/unit'
import { isObject, toPx } from '../utils/unit'

export default function (Glide, Components, Events) {
const Peek = {
Expand Down Expand Up @@ -31,10 +31,10 @@ export default function (Glide, Components, Events) {
*/
set (value) {
if (isObject(value)) {
value.before = toInt(value.before)
value.after = toInt(value.after)
value.before = toPx(value.before)
value.after = toPx(value.after)
} else {
value = toInt(value)
value = toPx(value)
}

Peek._v = value
Expand Down
19 changes: 19 additions & 0 deletions src/utils/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ export function toFloat (value) {
return parseFloat(value)
}

/**
* Converts a number or string of format /\d+(px|rem|em)?/
* to a number in pixel units
* @param {String|Number} value
* @param {Glide} glide
* @returns {Number}
*/
export function toPx (value) {
Copy link
Member

Choose a reason for hiding this comment

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

I think this logic here can be simplified following way:

export function toPx (value) {
  if (value.endsWith('rem')) {
    const { fontSize } = window.getComputedStyle(document.documentElement)
    return toInt(int * toFloat(fontSize))
  }

  return toInt(value)
}

Copy link
Member

Choose a reason for hiding this comment

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

Other than that looks LGTM :)

Copy link
Author

Choose a reason for hiding this comment

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

@jedrzejchalubek Sorry for getting back so late! This would unfortunately cause an error whenever value is not a string, though.

const int = toInt(value)
if (isNumber(value)) return int

if (value.endsWith('rem')) {
const { fontSize } = window.getComputedStyle(document.documentElement)
return toInt(int * toFloat(fontSize))
}

return int
}

/**
* Indicates whether the specified value is a string.
*
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/unit.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
toInt,
toPx,
isArray,
isString,
isObject,
Expand All @@ -15,6 +16,13 @@ describe('Function', () => {
expect(toInt('1px', 100)).toBe(1)
})

test('`toPx` should convert number, rem and px to pixel unit', () => {
document.documentElement.style.fontSize = '16px'
expect(toPx(1)).toBe(1)
expect(toPx('1px')).toBe(1)
expect(toPx('1rem')).toBe(16)
})

test('`isString` return `true` on valid string', () => {
expect(isString('undefined')).toBe(true)

Expand Down