1.0.4 • Published 3 years ago

@artifak/media v1.0.4

Weekly downloads
11
License
MIT
Repository
github
Last release
3 years ago

@artifak/media

Although Artifak helps you to write less media queries, there are also many other types of media queries available now which focuses more on querying devices and accessibilty. The Artifak Media library essentially helps with that and is a styled-component mixin to help you write media queries for your styled-components. Note that the CSS media query operator "not" is not supported for the time being. Docs are also available at Artifak Media.

Installation

Yarn

yarn add @artifak/media

NPM

npm install @artifak/media

Usage

media

Simply import media and the media query rule that you desire to use and include it in your styled component.

At its most basic usage, you can specify for example a min-width rule like so:

import { media } from '@artifak/media';

const MyArticle = styled.article`
  ${media({ width: '>= 30em' })`
    background: orange;
  `}
`;

Querying Media With "And"

For concatenation using the "and" keyword, below is a simple rule that would equate to "@media screen and (min-width: 30em)". Basically, just append the rules to the query object to execute a media query with "and" keywords.

import { media, screen } from '@artifak/media';

const MyArticle = styled.article`
  ${media({ screen, width: '>= 30em' })`
    background: orange;
  `}
`;

Querying Media With "Or"

For "or" statements, simply provide another query object as argument. Below will equate to: "@media screen and (min-width: 30em), (orientation: landscape)".

import { media, screen, landscape } from '@artifak/media';

const MyArticle = styled.article`
  ${media({ screen, width: '>= 30em' }, { landscape })`
    background: orange;
  `}
`;

Querying Media With Min, Max or Single Value

Keys to queries that offer min/max values are

  • width
  • height
  • ratio (which is for aspect-ratio)
  • res (which is for resolution)

For min values, use the ">=" operator.

import { media, screen } from '@artifak/media';

const MyArticle = styled.article`
  ${media({ screen, width: '>= 30em' })`
    background: orange;
  `}
`;

For max values, use the "<=" operator.

import { media, screen } from '@artifak/media';

const MyArticle = styled.article`
  ${media({ screen, width: '<= 30em' })`
    background: orange;
  `}
`;

For both min and max values as per below, is an example for min and max widths.

import { media, screen } from '@artifak/media';

const MyArticle = styled.article`
  ${media({ screen, width: '30em >= width <= 50em' })`
    background: orange;
  `}
`;

If for some reason you only want a single value for these properties, it can be applied like so.

import { media, screen } from '@artifak/media';

const MyArticle = styled.article`
  ${media({
    screen,
    width: '30em',
    height: '50em',
    ratio: '1/3',
    res: '72dpi'
  })`
    background: orange;
  `}
`;

Media Queries

Apart from the common width, height, aspect-ratio and resolution queries, below are a list of currently available media queries. Due to some newer queries not having sufficient browser support, they are omitted from the library for the time being.

Available Queries

Property NameValue
allall
screenscreen
onlyScreenonly screen
printprint
onlyPrintonly print
speechspeech
onlySpeechonly speech
hoverhover: hover
hoverNonehover: none
anyHoverany-hover: hover
anyHoverNoneany-hover: none
pointerpointer: pointer
pointerNonepointer: none
anyPointerany-pointer: pointer
anyPointerNoneany-pointer: none
displayFullScreendisplay-mode: fullscreen
displayMinUIdisplay-mode: minimal-ui
displayStandalonedisplay-mode: standalone
displayBrowserdisplay-mode: browser
portraitorientation: portrait
landscapeorientation: landscape
darkColorSchemeprefers-color-scheme: dark
lightColorSchemeprefers-color-scheme: light
reducedMotionprefers-reduced-motion: reduce
reducedMotionAnyprefers-reduced-motion: no-preference