🎠 Super-Query

A super media-query for styled-component. Intuitive and easy of use.

Build Status Coverage Status GitHub All Releases GitHub stars Known Vulnerabilities GitHub issues NPM Awesome

Description

We all know that Media Query is a very important part of your application; with that you can set any kind of responsiveness and device control.

Most of the developers only use simple media feature as part of the media query syntax, and for that simple use case, there's a bunch of good approach for your styled-component based application.

Media Feature examples: width | min-width | max-width | height | min-height | max-height


But how about control over the screen orientation, aspect-ratio, resolution and tv devices? Or even a interpolation between?

For those developers who need more control over the styled-component based application, we created the SuperQuery in order to provide a full powerful and simple control over your media query.


Want a simple code demonstration?

How about create a media query that handles screen widths between 360px and 1024px ?

const myStyledComponent = styled.div`
  ...
  ${SuperQuery().minWidth('360px').and().maxWidth('1024px').css`
    content: 'this is awesome!'
  `}
`

Cool, right? But it's even cooler to use our built-in breakpoints, let's rewrite it!

const myStyledComponent = styled.div`
  ...
  ${SperQuery().minWidth().sm().and().maxWidth().lg().css`
    content: 'this is even more awesome!'
  `}
`

Of even how about control the screen orientation over mobile devices ?

const myStyledComponent = styled.div`
  ...
  ${SuperQuery().maxWidth().md().and().landscape().css`
    content: 'Yep! Your device is on landscape mode!'
  `}
`

Want a more complex query ?


const myStyledComponent = styled.div`
  ...
  ${SuperQuery()
     .screen()
     .and()
     .deviceAspectRatio('16/9')
     .or()
     .screen()
     .and()
     .deviceAspectRatio('16/10')
     .css`
    content: 'Yep! Your device is on landscape mode!'
  `}
`

Yeah! Now you know how simple and powerful is SuperQuery!

Installation

Since we have experience some issues with NPM along the time, we strongly recomment the use of YARN Package Manage;

Download our NPM Package

For YARN users:

yarn add @themgoncalves/super-query

For NPM users:

npm install @themgoncalves/super-query

Note that it should not be in the devDependencies.


How to use

First we need to import the package into our component;

import SuperQuery from '@themgoncalves/super-query';

Then, we are ready to mix it with styled-component:

const Title = styled.h1`
  color: #666;
  font-size: 16px;
  ${SuperQuery().minWidth().lg().css`
    font-size: 20px;
  `};
  ${SuperQuery().minWidth().lg().and().landscape().css`
    font-size: 26px;
  `};
`;

See how easy to implement it is?

API Documentation

This package follows the css3 media query rule, click here to check it out.

The Media Query Syntax

A few motivating and useful examples of how your product can be used. Spice this up with code blocks and potentially more screenshots.

media_query: [[only | not]? <media_type> [ and <expression> ]*] |
             <expression> [ and <expression> ]*

expression: ( <media_feature> [: <value>]? )

media_type: all | aural | braille | handheld | print |
  projection | screen | tty | tv | embossed

media_feature: width | min-width | max-width
  | height | min-height | max-height
  | device-width | min-device-width | max-device-width
  | device-height | min-device-height | max-device-height
  | aspect-ratio | min-aspect-ratio | max-aspect-ratio
  | device-aspect-ratio | min-device-aspect-ratio | max-device-aspect-ratio
  | color | min-color | max-color
  | color-index | min-color-index | max-color-index
  | monochrome | min-monochrome | max-monochrome
  | resolution | min-resolution | max-resolution
  | scan | grid

Built-in Breakpoints

Type How to Use Size Comes after of Following options Can set css after?
xs .xs() 0px Media Feature Logical Operator βœ”
sm .sm() 576px Media Feature Logical Operator βœ”
md .md() 768px Media Feature Logical Operator βœ”
lg .lg() 992px Media Feature Logical Operator βœ”
xl .xl() 1200px Media Feature Logical Operator βœ”

Media Type

Type How to Use Comes after of Following options Can set css after?
all .all() SuperQuery(), Logical Operator Logical Operator, Media Feature βœ–
aural .aural() SuperQuery(), Logical Operator Logical Operator, Media Feature βœ–
braille .braille() SuperQuery(), Logical Operator Logical Operator, Media Feature βœ–
handheld .handheld() SuperQuery(), Logical Operator Logical Operator, Media Feature βœ–
print .print() SuperQuery(), Logical Operator Logical Operator, Media Feature βœ–
projection .projection() SuperQuery(), Logical Operator Logical Operator, Media Feature βœ–
screen .screen() SuperQuery(), Logical Operator Logical Operator, Media Feature βœ–
tty .tty() SuperQuery(), Logical Operator Logical Operator, Media Feature βœ–
tv .tv() SuperQuery(), Logical Operator Logical Operator, Media Feature βœ–
embossed .embossed() SuperQuery(), Logical Operator Logical Operator, Media Feature βœ–

Media Feature

Type How to Use Optional Argument Example Comes after of Following options Can set css after?
width .width() .width('100px') SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
min-width .minWidth() .minWidth('100px') SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
max-width .maxWidth() .maxWidth('100px') SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
height .height() .height('340px') SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
min-height .minHeight() .minHeight('340px') SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
max-height .maxHeight() .maxHeight('340px') SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
device-width .deviceWidth() .deviceWidth('960px') SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
min-device-width .minDeviceWidth() .minDeviceWidth('960px') SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
max-device-width .maxDeviceWidth() .maxDeviceWidth('960px') SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
device-height .height() .height('320px') SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
min-device-height .minDeviceHeight() .minDeviceHeight('340px') SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
max-device-height .maxDeviceHeight() .maxDeviceHeight('340px') SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
aspect-ratio .aspectRatio() .aspectRatio('1/1') SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
min-aspect-ratio .minAspectRatio() .minAspectRatio('1/1') SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
max-aspect-ratio .maxAspectRatio() .maxAspectRatio('1/1') SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
device-aspect-ratio .deviceAspectRatio() .deviceAspectRatio('16/9') SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
min-device-aspect-ratio .minDeviceAspectRatio() .minDeviceAspectRatio('16/9') SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
max-device-aspect-ratio .maxDeviceAspectRatio() .maxDeviceAspectRatio('16/9') SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
color .color() n/a SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
min-color .minColor() .minColor('4') SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
max-color .maxColor() .maxColor('4') SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
color-index .colorIndex() .colorIndex('256') SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
min-color-index .minColorIndex() .minColorIndex('256') SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
max-color-index .maxColorIndex() .maxColorIndex('256') SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
monochrome .monochrome() n/a SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
min-monochrome .minMonochrome() .minMonochrome('8') SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
max-monochrome .maxMonochrome() .maxMonochrome('8') SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
resolution .resolution() .resolution('300dpi') SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
min-resolution .minResolution() .minResolution('300dpi') SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
max-resolution .maxResolution() .maxResolution('300dpi') SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
scan .scan() .scan('progressive') SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”
grid .grid() n/a SuperQuery(), Logical Operator, Media Type Logical Operator, Breakpoints βœ”

Screen Orientation

Type How to Use Comes after of Following options Can set css after?
landscape .landscape() Logical Operator Logical Operator βœ”
portrait .portrait() Logical Operator Logical Operator βœ”

Logical Operator

Type How to Use Comes after of Following options Can set css after?
and .and() Media Feature, Screen Orientation, Breakpoints Media Type, Media Feature, Screen Orientation βœ–
or .or() Media Feature, Screen Orientation, Breakpoints Media Type, Media Feature, Screen Orientation βœ–
not .not() SuperQuery() Media Type βœ–
only .only() SuperQuery() Media Type βœ–

Render CSS

Simply call the css function as the last iteration and pass the css syntax throw ES6 Tagged Template Literals:

const List = styled.ul`
  list-style: none;
  padding: 0;
  margin: 0;
  SuperQuery().minWidth().md().css`
    color: white;
    font-size: 14px;
    text-decoration: none;
  `
`

How to create custom breakpoints?

One of the coolest features we have on SuperQuery is the possibility to override our default breakpoints into your own custom.

Here is how to do:

import SuperQuery from '@themgoncalves/super-query';

// Here is an example of a custom breakpoint
const customBreakpoints = {
  extraSmall: 360,
  small: 640,
  medium: 960,
  large: 1024,
  extraLarge: 1200,
  superExtraLarge: 1600,
};

// Then just import your custom breakpoints into the `SuperQuery`
// and you are Zready start use it!
const Title = styled.h1`
  color: #666;
  font-size: 16px;
  ${SuperQuery(customBreakpoints).minWidth().superExtraLarge().css`
    font-size: 20px;
  `};
`;

// Or your custom breakpoints directly from `ThemeProvider`
const Title = styled.h1`
  color: #666;
  font-size: 16px;
  ${props => SuperQuery(props.theme.breakpoints).minWidth().superExtraLarge().css`
    font-size: 20px;
  `};
`;


NEW: Orientation

We have added in the version 1.0.0 a new feature: Orientation, which is a Screen Orientation manager.

This feature is an implementation of the Web API Screen.orientation available in the modern browser.

Note that this feature might not work on a several environments, like in the iOS. For more information, click here and check the browser compatibility

How to use

import { Orientation } from '@themgoncalves/super-query';

// or you can import direct the module

import Orientation from '@themgoncalves/super-query/orientation';

Listening for screen orientation change

Orientation.onChange((orientation) => {
    console.inf('Screen orientation has changed to: ', orientation);
});

Get the current orientation

const currentOrientation = Orientation.current();
console.info('Current screen orientation: ', currentOrientation);

Locking the orientation

// screen orientations available to be locked:
// landscape-primary
// landscape-secondary
// portrait-primary
// portrait-secondary

const wasScreenLocked = Orientation.lock('portrait-primary');

// or you can pass an array with the orientations to be locked
const wasScreenLocked = Orientation.lock(['portrait-primary', 'portrait-secondary']);

console.info('Was screen locked? ', wasScreenLocked);

Unlocking the screen

const wasScreenUnlocked = Orientation.unlock();
console.info('Was screen unlocked? ', wasScreenUnlocked);

Check if the screen orientation was locked before

const wasScreenOrientationLocked = Orientation.isLocked();
console.info('Was screen orientation locked before? ', wasScreenOrientationLocked);


Release History

  • 2.0.0
    • NEW: Option to override bult-in breakpoints directly from SuperQuery(myCustombreakpoints)
    • DEPRECATED: configureBreakpoints() to set custom breakpoints, use SuperQuery(myCustombreakpoints) instead
  • 1.0.0
    • Stable version
    • NEW: Created Orientation - Screen Orientation management
    • NEW: Created Example (Demo) of Super-Query
    • Improved folder structure
  • 0.1.1
    • Fixed typo in the ToString method used for tests
    • Minor fixes and improvement in the project's documentation
  • 0.1.0
    • First release
    • NEW: Created configureBreakpoints() to set custom breakpoints
  • 0.0.1
    • Work in progress


Meta

Author

Marcos GonΓ§alves – LinkedIn – Website

License

Distributed under the MIT license. Click here for more information.

https://github.com/themgoncalves/super-query

Contributing

  1. Fork it (https://github.com/themgoncalves/super-query/fork)
  2. Create your feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -m ':zap: Add some fooBar')
  4. Push to the branch (git push origin feature/fooBar)
  5. Create a new Pull Request