@1natsu/handy-media-query 中文文档教程

发布于 6年前 浏览 27 项目主页 更新于 3年前

一种方便的 JavaScript CSS 媒体查询方法

只需返回媒体查询字符串。

一起使用……(例如样式化组件、情感……)

Table of contents

✨ Getting Started

有机会与 yarn

yarn add @1natsu/handy-media-query

npm

npm install @1natsu/handy-media-query

Usage

Simple

import mq from '@1natsu/handy-media-query'

mq.max(480) // "@media all and (max-width: 30em)"
mq.between(481,767) // "@media all and (min-width: 30.0625em) and (max-width: 47.9375em)"
mq.min(768) // "@media all and (min-width: 48em)"

默认情况下,将传递的 px 转换为 em。

Realistic example

maybe as usufull

mediaQuery.js

import mq, { composeMediaQuery, pxToEm } from '@1natsu/handy-media-query'

const breakpoints = {
  small: 480,
  middle: [481, 767],
  large: 768
}

const mediaQueries = composeMediaQuery({
  small: mq.max(breakpoints.small),
  middle: mq.between(breakpoints.middle[0], breakpoints.middle[1]),
  large: mq.min(breakpoints.large),
  irregular: `@media (min-height: ${pxToEm('680px')}), screen and (orientation: portrait)`
})

export { mediaQueries as mq }

Foo.jsx

作为示例,带有 Styled-components(v4) 的组件文件

import React from 'react'
import styled from "styled-components";
import { mq } from './mediaQuery'


const Foo = ({text}) => <div>{text}</div>

const StyledFoo = styled(Foo)`
  color: blue;
  ${mq.small} {
    color: red;
  }
  ${mq.middle} {
    color: cyan;
  }
  // Local irregular……is also OK
  ${mq.between(1921,3840)} {
    color: pink;
  }
`

APIs

Default exported object

import mq from '@1natsu/handy-media-query'

#min(minPx, [opts])

namerequiretypedefaultdecstiption
minPxstring | number-"<number>px" or number(assuming pixels)
options-objectDefaultOptions
mq.min(480)
// '@media all and (min-width: 30em)'

mq.min(480, {unit: 'px', mediaType: 'screen'})
// '@media screen and (min-width: 480px)'

#max(maxPx, [opts])

namerequiretypedefaultdecstiption
maxPxstring | number-"<number>px" or number(assuming pixels)
options-objectDefaultOptions
mq.max(480)
// '@media all and (max-width: 30em)'

mq.max(480, {unit: 'px', mediaType: 'screen'})
// '@media screen and (max-width: 480px)'

#between(minPx, maxPx, [opts])

namerequiretypedefaultdecstiption
minPxstring | number-"<number>px" or number(assuming pixels)
maxPxstring | number-"<number>px" or number(assuming pixels)
options-objectDefaultOptions
mq.between(480,980)
// '@media all and (min-width: 30em) and (max-width: 61.25em)'

mq.between(480, 980, {unit: 'px', mediaType: 'screen'})
// '@media screen and (min-width: 480px) and (max-width: 61.25em)'
DefaultOptions
namerequiretypedefaultdecstiption
unit-'em' | 'rem' | 'px''em'Output unit
unitRatio-number16Unit ratio as a reference of 'em' or 'rem'
mediaType-'all' | 'screen' | 'print' | 'speech''all'MDN - Media Query#Media-Types

Named exported methods

composeMediaQuery(userSelfMediaQuery)

返回带有 {passed argument object} 的新对象+ {media query methods}

这个函数合并了媒体查询方法 & 与对象一起传递。 因此有必要避免重复的属性名称。

namerequiretypedefaultdecstiption
userSelfMediaQueryobject-Object with string media query in value
import { composeMediaQuery } from '@1natsu/handy-media-query'

const mediaQueries = composeMediaQuery({
  small: '@media (min-width: 320px)'
  middle: '@media (min-width: 768px)'
  large: '@media (min-width: 1280px)'
  irregular: `@media (min-height: 400px), screen and (orientation: portrait)`
})

↓↓ 生成的对象的内容如下↓↓

const mediaQueries = {
  small: '@media (min-width: 320px)'
  middle: '@media (min-width: 768px)'
  large: '@media (min-width: 1280px)'
  irregular: '@media (min-height: 400px), screen and (orientation: portrait)'
  min: [Function]
  max: [Function]
  between: [Function]
  …
  …
  …
}

但是,假设像这样的用法 现实的例子

pxToEm(value, ratio)

  • Unit converter for px to em
namerequiretypedefaultdecstiption
valuestring | number-Conversion source px.
"<number>px" or number
ratio-number16Unit ratio as a reference of 'em'
import { pxToEm } from '@1natsu/handy-media-query'

const convertToEm = pxToEm(480, 10)
console.log(convertToEm) // 48em

pxToRem(value, ratio)

  • Unit converter for px to rem
namerequiretypedefaultdecstiption
valuestring | number-Conversion source px.
"<number>px" or number
ratio-number16Unit ratio as a reference of 'rem'
import { pxToRem } from '@1natsu/handy-media-query'

const convertToRem = pxToRem(480, 10)
console.log(convertToRem) // 48rem

Running the tests

Jest

yarn test

npm run test

Versioning

使用 SemVer 进行版本控制。 有关可用版本,请参阅此存储库上的标签

©️ License

麻省理工学院 © 1natsu172

Acknowledgments

Inspiration

A handy CSS media query methods of JavaScript

Just return the media query string.

The opportunity to use… (e.g. styled-components, emotion, …)

???? Table of contents

✨ Getting Started

with yarn

yarn add @1natsu/handy-media-query

or

with npm

npm install @1natsu/handy-media-query

???? Usage

Simple

import mq from '@1natsu/handy-media-query'

mq.max(480) // "@media all and (max-width: 30em)"
mq.between(481,767) // "@media all and (min-width: 30.0625em) and (max-width: 47.9375em)"
mq.min(768) // "@media all and (min-width: 48em)"

By default, the passed px is converted to em.

???? Realistic example

maybe as usufull

mediaQuery.js

import mq, { composeMediaQuery, pxToEm } from '@1natsu/handy-media-query'

const breakpoints = {
  small: 480,
  middle: [481, 767],
  large: 768
}

const mediaQueries = composeMediaQuery({
  small: mq.max(breakpoints.small),
  middle: mq.between(breakpoints.middle[0], breakpoints.middle[1]),
  large: mq.min(breakpoints.large),
  irregular: `@media (min-height: ${pxToEm('680px')}), screen and (orientation: portrait)`
})

export { mediaQueries as mq }

Foo.jsx

As an example a component file with Styled-components(v4)

import React from 'react'
import styled from "styled-components";
import { mq } from './mediaQuery'


const Foo = ({text}) => <div>{text}</div>

const StyledFoo = styled(Foo)`
  color: blue;
  ${mq.small} {
    color: red;
  }
  ${mq.middle} {
    color: cyan;
  }
  // Local irregular……is also OK
  ${mq.between(1921,3840)} {
    color: pink;
  }
`

???? APIs

Default exported object

import mq from '@1natsu/handy-media-query'

#min(minPx, [opts])

namerequiretypedefaultdecstiption
minPxstring | number-"<number>px" or number(assuming pixels)
options-objectDefaultOptions
mq.min(480)
// '@media all and (min-width: 30em)'

mq.min(480, {unit: 'px', mediaType: 'screen'})
// '@media screen and (min-width: 480px)'

#max(maxPx, [opts])

namerequiretypedefaultdecstiption
maxPxstring | number-"<number>px" or number(assuming pixels)
options-objectDefaultOptions
mq.max(480)
// '@media all and (max-width: 30em)'

mq.max(480, {unit: 'px', mediaType: 'screen'})
// '@media screen and (max-width: 480px)'

#between(minPx, maxPx, [opts])

namerequiretypedefaultdecstiption
minPxstring | number-"<number>px" or number(assuming pixels)
maxPxstring | number-"<number>px" or number(assuming pixels)
options-objectDefaultOptions
mq.between(480,980)
// '@media all and (min-width: 30em) and (max-width: 61.25em)'

mq.between(480, 980, {unit: 'px', mediaType: 'screen'})
// '@media screen and (min-width: 480px) and (max-width: 61.25em)'
DefaultOptions
namerequiretypedefaultdecstiption
unit-'em' | 'rem' | 'px''em'Output unit
unitRatio-number16Unit ratio as a reference of 'em' or 'rem'
mediaType-'all' | 'screen' | 'print' | 'speech''all'MDN - Media Query#Media-Types

Named exported methods

composeMediaQuery(userSelfMediaQuery)

Return a new object with {passed argument object} + {media query methods}

This function merges media query methods & passed with the object. So it's necessary to avoid duplicate property names.

namerequiretypedefaultdecstiption
userSelfMediaQueryobject-Object with string media query in value
import { composeMediaQuery } from '@1natsu/handy-media-query'

const mediaQueries = composeMediaQuery({
  small: '@media (min-width: 320px)'
  middle: '@media (min-width: 768px)'
  large: '@media (min-width: 1280px)'
  irregular: `@media (min-height: 400px), screen and (orientation: portrait)`
})

↓↓ The contents of the generated object are as follows ↓↓

const mediaQueries = {
  small: '@media (min-width: 320px)'
  middle: '@media (min-width: 768px)'
  large: '@media (min-width: 1280px)'
  irregular: '@media (min-height: 400px), screen and (orientation: portrait)'
  min: [Function]
  max: [Function]
  between: [Function]
  …
  …
  …
}

But, assume a usage like a Realistic example

pxToEm(value, ratio)

  • Unit converter for px to em
namerequiretypedefaultdecstiption
valuestring | number-Conversion source px.
"<number>px" or number
ratio-number16Unit ratio as a reference of 'em'
import { pxToEm } from '@1natsu/handy-media-query'

const convertToEm = pxToEm(480, 10)
console.log(convertToEm) // 48em

pxToRem(value, ratio)

  • Unit converter for px to rem
namerequiretypedefaultdecstiption
valuestring | number-Conversion source px.
"<number>px" or number
ratio-number16Unit ratio as a reference of 'rem'
import { pxToRem } from '@1natsu/handy-media-query'

const convertToRem = pxToRem(480, 10)
console.log(convertToRem) // 48rem

???? Running the tests

with Jest.

yarn test

or

npm run test

???? Versioning

Use SemVer for versioning. For the versions available, see the tags on this repository.

©️ License

MIT © 1natsu172

???? Acknowledgments

Inspiration

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文