-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
cole
committed
Jul 30, 2024
1 parent
9a4fbd0
commit b9a5e5c
Showing
7 changed files
with
134 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,112 @@ | ||
const defaultValues = { | ||
xs: [null, 575], | ||
sm: [576, 767], | ||
md: [768, 991], | ||
lg: [992, 1199], | ||
xl: [1200, 1599], | ||
xxl: [1600, null], | ||
__order__: ['xs', 'sm', 'md', 'lg', 'xl', 'xxl'] | ||
import { inject, reactive } from 'vue' | ||
import { addClass, getWindowSize, on, removeClass } from '../../utils/dom' | ||
import { debounce, pick } from 'lodash-es' | ||
|
||
const SIZE_LIST = ['xs', 'sm', 'md', 'lg', 'xl', 'xxl'] | ||
|
||
const BaseKey = Symbol('Screen') | ||
|
||
export function createScreen (options) { | ||
const { sizes = {}, delay = 16, classes } = options || {} | ||
|
||
const baseSizes = { | ||
xs: 0, | ||
sm: 576, | ||
md: 768, | ||
lg: 992, | ||
xl: 1200, | ||
xxl: 1600 | ||
} | ||
|
||
const needSizes = pick({ ...baseSizes, ...sizes }, SIZE_LIST) | ||
|
||
const screen = reactive({ | ||
width: 0, | ||
height: 0, | ||
name: 'xs', | ||
sizes: needSizes, | ||
lt: { | ||
sm: true, | ||
md: true, | ||
lg: true, | ||
xl: true, | ||
xxl: true | ||
}, | ||
gt: { | ||
xs: false, | ||
sm: false, | ||
md: false, | ||
lg: false, | ||
xl: false | ||
}, | ||
xs: true, | ||
sm: false, | ||
md: false, | ||
lg: false, | ||
xl: false, | ||
xxl: false | ||
}) | ||
|
||
function update () { | ||
const [width, height] = getWindowSize() | ||
const { sizes } = screen | ||
|
||
screen.width = width | ||
screen.height = height | ||
|
||
screen.lt.sm = width < sizes.sm | ||
screen.lt.md = width < sizes.md | ||
screen.lt.lg = width < sizes.lg | ||
screen.lt.xl = width < sizes.xl | ||
screen.lt.xxl = width < sizes.xxl | ||
|
||
screen.gt.xs = width >= sizes.sm | ||
screen.gt.sm = width >= sizes.md | ||
screen.gt.md = width >= sizes.lg | ||
screen.gt.lg = width >= sizes.xl | ||
screen.gt.xl = width >= sizes.xxl | ||
|
||
screen.xs = screen.lt.sm | ||
screen.sm = screen.gt.xs === false && screen.lt.md === true | ||
screen.md = screen.gt.sm === false && screen.lt.lg === true | ||
screen.lg = screen.gt.md === false && screen.lt.xl === true | ||
screen.xl = screen.gt.lg === false && screen.lt.xxl === true | ||
screen.xxl = screen.gt.xl | ||
|
||
const name = (screen.xs === true && 'xs') | ||
|| (screen.sm === true && 'sm') | ||
|| (screen.md === true && 'md') | ||
|| (screen.lg === true && 'lg') | ||
|| (screen.xl === true && 'xl') | ||
|| 'xxl' | ||
|
||
if (name !== screen.name) { | ||
if (classes === true) { | ||
removeClass(document.body, `screen--${screen.name}`) | ||
addClass(document.body, `screen--${name}`) | ||
} | ||
screen.name = name | ||
} | ||
} | ||
|
||
const updateEvent = debounce(update, delay) | ||
|
||
// @todo visualViewport | ||
on(window, 'resize', updateEvent, { passive: true }) | ||
|
||
update() | ||
|
||
if (classes === true && screen.name === 'xs') { | ||
addClass(document.body, `screen--xs`) | ||
} | ||
|
||
return { | ||
install: (app) => { | ||
app.provide(BaseKey, screen) | ||
} | ||
} | ||
} | ||
|
||
export function useScreen () { | ||
return inject(BaseKey, {}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { createScreen } from '@packages/plugins/screen' | ||
|
||
export default ({ app }) => { | ||
const screen = createScreen({ classes: true }) | ||
// 屏幕插件 | ||
app.use(screen) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters