Skip to content

Commit 1ee34bf

Browse files
committed
feat: add computed breakpoints
1 parent 65b4745 commit 1ee34bf

File tree

15 files changed

+202
-58
lines changed

15 files changed

+202
-58
lines changed

.github/workflows/node.js.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,19 @@ jobs:
2121

2222
steps:
2323
- uses: actions/checkout@v2
24+
2425
- name: Use Node.js ${{ matrix.node-version }}
2526
uses: actions/setup-node@v1
2627
with:
2728
node-version: ${{ matrix.node-version }}
29+
2830
- run: npm install -g lerna start-server-and-test
2931
- run: lerna bootstrap
3032
- run: lerna run --scope vue-screen build --stream
3133
- run: lerna run --scope vue-screen test --stream
3234
- run: start-server-and-test "lerna run --scope vue-screen-examples serve --stream" http://localhost:8081 "lerna run --scope vue-screen-e2e cy:run --stream"
35+
36+
- name: Coverage
37+
uses: codecov/codecov-action@v1
38+
with:
39+
token: ${{ secrets.CODECOV_TOKEN }}

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ dist
33
!packages/docs/src/.vitepress/dist
44
.nuxt
55
packages/e2e/cypress/screenshots
6-
.DS_Store
6+
.DS_Store
7+
coverage

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
[![Build Status](https://github.com/reegodev/vue-screen/workflows/Node.js%20CI/badge.svg)](https://github.com/reegodev/vue-screen/actions)
33
[![npm version](https://img.shields.io/npm/v/vue-screen/next)](https://www.npmjs.com/package/vue-screen)
44
[![npm downloads](https://img.shields.io/npm/dm/vue-screen)](https://www.npmjs.com/package/vue-screen)
5+
[![codecov](https://codecov.io/gh/reegodev/vue-screen/branch/master/graph/badge.svg?token=KTHOOUSHFJ)](https://codecov.io/gh/reegodev/vue-screen)
56

67
<br>
78

packages/docs/src/.vitepress/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ if (process.env.NODE_ENV === 'production') {
1616
}
1717

1818
module.exports = {
19-
base: '/vue-screen/',
19+
// base: '/vue-screen/',
2020
lang: 'en-US',
2121
title: 'Vue-Screen',
2222
description: '"Reactive screen size and media query states for Vue components. Integrates with most UI frameworks out of the box.',

packages/docs/src/guide/configuration/composition-api.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,50 @@ The value can either be a string literal, with one of the supported UI framework
5757
or an object that specifies a custom grid:
5858
5959
```ts
60-
Record<string, number | string>
60+
Record<string, number | string | ComputedBreakpoint>
6161
```
6262
6363
For example:
6464
```js
65+
import { useGrid } from 'vue-screen'
66+
6567
useGrid({
6668
phone: '340px',
6769
tablet: 768,
6870
desktop: '32em',
6971
})
7072
```
73+
<br>
74+
75+
#### Computed breakpoints
76+
77+
Aside from using direct breakpoint values, you can also specify breakpoints that depend on other breakpoints, for example:
78+
79+
```js
80+
import { useGrid } from 'vue-screen'
7181

82+
useGrid({
83+
phone: '340px',
84+
tablet: 768,
85+
desktop: '32em',
86+
tabletAndDown: grid => !grid.desktop,
87+
})
88+
```
89+
90+
In this example, the breakpoint `tabletAndDown` will be recalculated everytime `desktop` breakpoint changes.
91+
92+
<br>
93+
94+
#### Extending default UI frameworks breakpoints
95+
96+
Sometimes it's useful to add new breakpoints to the default configuration of one of the supported UI frameworks.<br>
97+
You can do that by using the `extendGrid` helper:
98+
99+
```js
100+
import { useGrid, extendGrid } from 'vue-screen'
101+
102+
useGrid(extendGrid('tailwind', {
103+
mdAndDown: grid => !grid.lg
104+
}))
105+
```
72106

packages/docs/src/guide/upgrading.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ editLink: true
66

77
## Configuration
88
v2 has brought a few breaking changes on the configuration.
9-
To see how to configure the libary please refer to the [configuration section](/vue-screen/guide/configuration/composition-api).<br>
10-
Callbacks have been removed and there is no plan on adding them back at the moment.
9+
To see how to configure the library please refer to the [configuration section](/vue-screen/guide/configuration/composition-api).<br>
1110

1211
## API
1312

packages/docs/src/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ editLink: true
33
title: Reactive screen size and media query states for VueJs
44
---
55
::: warning
6-
Version 2.0 only supports Vue 3 and is in early alpha version. There might still be API changes before final release.<br> v1 docs are available [here](https://github.com/reegodev/vue-screen/tree/v1.5.3#vuescreen)
6+
Version 2.0 only supports Vue 3 and is still in alpha.<br> v1 docs are available [here](https://github.com/reegodev/vue-screen/tree/v1.5.3#vuescreen)
77
:::
88
<br>
99
<div style="text-align: center">

packages/lib/__tests__/useGrid.ts

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import {
22
createGridObject,
33
createConfigFromLiteral,
4-
useGrid
4+
useGrid,
5+
extendGrid,
6+
getCurrentBreakpoint
57
} from '../src/useGrid'
68

79
import { GridDefinitionLiteral } from '../src/types/grid'
@@ -38,7 +40,7 @@ describe('useGrid', () => {
3840
expect(Object.values(gridObject).every(value => value === false)).toBe(true)
3941
})
4042

41-
it('creates a reactive grid object', async () => {
43+
it('creates a reactive grid object from a literal', async () => {
4244
const grid = useGrid('bulma')
4345

4446
expect(grid.tablet).toBe(false)
@@ -51,4 +53,62 @@ describe('useGrid', () => {
5153
expect(grid.tablet).toBe(true)
5254
})
5355

56+
it('creates a reactive grid object from a custom config', async () => {
57+
const grid = useGrid({
58+
a: 0,
59+
b: 1,
60+
c: 2,
61+
})
62+
63+
expect(grid.a).toBe(false)
64+
65+
await new Promise((resolve) => {
66+
grid.a = true
67+
watchEffect(() => resolve(grid))
68+
})
69+
70+
expect(grid.a).toBe(true)
71+
})
72+
73+
it('extends a grid literal config', async () => {
74+
const grid = useGrid(extendGrid('bulma', {
75+
tabletAndBelow: grid => !grid.desktop
76+
}))
77+
78+
const allBreakpoints = Object.keys(grids['bulma']).concat(['breakpoint', 'tabletAndBelow'])
79+
expect(Object.keys(grid).every(key => allBreakpoints.includes(key))).toBe(true)
80+
})
81+
82+
})
83+
84+
describe('getCurrentBreakpoint', () => {
85+
it('returns an empty value if no breakpoint is active', () => {
86+
const config = {
87+
a: 0,
88+
b: 1,
89+
c: 2
90+
}
91+
const gridObject = {
92+
a: false,
93+
b: false,
94+
c: false,
95+
}
96+
97+
expect(getCurrentBreakpoint(config, gridObject)).toBe('')
98+
})
99+
100+
it('returns the currently active breakpoint', () => {
101+
const config = {
102+
a: 0,
103+
b: 1,
104+
c: 2
105+
}
106+
const gridObject = {
107+
a: false,
108+
b: true,
109+
c: true,
110+
}
111+
112+
expect(getCurrentBreakpoint(config, gridObject)).toBe('c')
113+
})
54114
})

packages/lib/__tests__/useScreen.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('useScreen', () => {
3333
touch: false,
3434
} as ScreenConfig
3535

36-
const screen = useScreen(config)
36+
const screen = useScreen(config, 300)
3737

3838
expect(screen).toStrictEqual({
3939
resolution: `${config.width}x${config.height}`,

packages/lib/jest.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ module.exports = {
1010
"^.+\\.(ts|tsx)$": "ts-jest"
1111
},
1212
"testEnvironment": "node",
13+
collectCoverage: true,
14+
coveragePathIgnorePatterns: ['src/utils.ts', 'src/grids'],
15+
coverageReporters: ['lcov', 'html', 'text'],
1316
}

0 commit comments

Comments
 (0)