Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WB-1655: Add mediaQuery breakpoints to wonder-blocks-tokens #2349

Merged
merged 15 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/few-paws-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/wonder-blocks-tokens": minor
---

Add mediaQuery tokens for viewport sizing
1 change: 1 addition & 0 deletions __docs__/wonder-blocks-tokens/__overview__.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ These represent the design decisions at Khan Academy, such as:
- <a href="/?path=/docs/packages-tokens-color--docs">Color Primitives</a>
- <a href="/?path=/docs/packages-tokens-spacing--docs">Spacing</a>
- <a href="/?path=/docs/packages-tokens-typography--docs">Typography</a>
- <a href="/?path=/docs/packages-tokens-media-queries--docs">Media Queries</a>

## Usage

Expand Down
82 changes: 82 additions & 0 deletions __docs__/wonder-blocks-tokens/tokens-media-queries.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import {Meta, Source} from "@storybook/blocks";

import TokenTable from "../../.storybook/components/token-table";

import {View} from "@khanacademy/wonder-blocks-core";
import * as tokens from "@khanacademy/wonder-blocks-tokens";

import ComponentInfo from "../../.storybook/components/component-info";
import packageConfig from "../../packages/wonder-blocks-tokens/package.json";

<Meta title="Packages / Tokens / Media Query Breakpoints" />

# Media Queries

<ComponentInfo name={packageConfig.name} version={packageConfig.version} />

All the available media query breakpoint values and pure widths that can be used for min-width,
max-width, width, etc.

## Usage

### Media Queries

You can use mediaQuery conditions by importing `breakpoint` from the
`wonder-blocks-tokens` package and accessing its object properties for sizing like so:
`breakpoint.mediaQuery.sm`.

```js
import {breakpoint} from "@khanacademy/wonder-blocks-tokens";
const styles = {
[breakpoint.mediaQuery.sm]: {
flexDirection: "column"
}
};
```

### Pure widths

marcysutton marked this conversation as resolved.
Show resolved Hide resolved
You can also use pure width values by importing `breakpoint` from the
`wonder-blocks-tokens` package and accessing the `width` object:
`breakpoint.width.sm`. These can be useful for tooling or in CSS where a number value
is needed. Therefore, pixel values are returned without a unit. You can interpolate
a string to add the `px` unit like so:

```js
import {breakpoint} from "@khanacademy/wonder-blocks-tokens";
const styles = {
element: {
maxWidth: `${breakpoint.width.lg}px`
}
};
```

## Tokens

<TokenTable
columns={[
{
label: "Media Query Token",
cell: (row) => <code>breakpoint.mediaQuery.{row.label}</code>,
},
{
label: "Value",
cell: "value",
},
]}
tokens={tokens.breakpoint.mediaQuery}
/>

<TokenTable
columns={[
{
label: "Width Token",
cell: (row) => <code>breakpoint.width.{row.label}</code>,
},
{
label: "Value (px)",
cell: "value",
},
]}
tokens={tokens.breakpoint.width}
/>
6 changes: 6 additions & 0 deletions packages/wonder-blocks-tokens/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {color} from "./tokens/color";
import {font} from "./tokens/font";
import {spacing} from "./tokens/spacing";

// media queries
import {breakpoint} from "./tokens/media-queries";
// semantic tokens
import {semanticColor} from "./tokens/semantic-color";

Expand All @@ -18,6 +20,10 @@ export {
color,
font,
spacing,
/**
* Media query breakpoints.
*/
breakpoint,
/**
* Semantic tokens.
*/
Expand Down
40 changes: 40 additions & 0 deletions packages/wonder-blocks-tokens/src/tokens/media-queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* A default set of media queries to use for different screen sizes.
*
* Breakpoint documentation: https://khanacademy.atlassian.net/wiki/spaces/WB/pages/2099970518/Layout+Breakpoints
*
*/

/* Pure width values */
const width = {
xsMax: 567,
smMin: 568,
smMax: 681,
mdMin: 682,
mdMax: 1023,
lgMin: 1024,
} as const;

/* Named mediaQuery conditions */
const mediaQuery = {
// Note: any updates to this will need to be replicated in /types/aphrodite.d.ts
xs: `@media screen and (max-width: ${width.xsMax}px) /* breakpoint.mediaQuery.xs */`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is /* breakpoint.mediaQuery.xs */ useful for IDE auto completion or something else?

I was looking at the Docs and that info appears duplicated in the table.

Screenshot 2024-11-04 at 12 02 33 PM

Again, if this is helpful for DX reasons, you can surely leave it as it is.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question! It was a trick to avoid conflicts for typing of strings. I included it based on this comment in webapp from @kevinb-khan: https://github.com/Khan/webapp/blob/55c4465017a829d53bdc6d00d94d6d93e3b5dfe4/services/static/javascript/shared-styles-package/media-queries.ts#L26

We attach a unique comment string to each of the media queries in this file. That way, if another media query has the same constraints as one of these queries, their strings won't be identical, and they therefore won't conflict.

Admittedly, the typing for this was really hard to get right and I still wasn't that happy with it in the end. It would be one of the first things I'd want to refactor for the Aphrodite successor.

sm: `@media screen and (min-width: ${width.smMin}px) and (max-width: ${width.smMax}px) /* breakpoint.mediaQuery.sm */`,
md: `@media screen and (min-width: ${width.mdMin}px) and (max-width: ${width.mdMax}px) /* breakpoint.mediaQuery.md */`,
lg: `@media screen and (min-width: ${width.mdMin}px) and (max-width: ${width.lgMin}px) /* breakpoint.mediaQuery.lg */`,
xl: `@media screen and (min-width: ${width.lgMin}px) /* breakpoint.mediaQuery.xl */`,

xsOrSmaller: `@media screen and (max-width: ${width.xsMax}px) /* breakpoint.mediaQuery.xsOrSmaller */`,
smOrSmaller: `@media screen and (max-width: ${width.smMax}px) /* breakpoint.mediaQuery.smOrSmaller */`,
mdOrSmaller: `@media screen and (max-width: ${width.mdMax}px) /* breakpoint.mediaQuery.mdOrSmaller */`,
lgOrSmaller: `@media screen and (max-width: ${width.lgMin}px) /* breakpoint.mediaQuery.lgOrSmaller */`,

smOrLarger: `@media screen and (min-width: ${width.smMin}px) /* breakpoint.mediaQuery.smOrLarger */`,
mdOrLarger: `@media screen and (min-width: ${width.mdMin}px) /* breakpoint.mediaQuery.mdOrLarger */`,
lgOrLarger: `@media screen and (min-width: ${width.lgMin}px) /* breakpoint.mediaQuery.lgOrLarger */`,
} as const;

export const breakpoint = {
width,
mediaQuery,
};
39 changes: 39 additions & 0 deletions types/aphrodite.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,32 @@
declare module "aphrodite" {
import * as React from "react";

// Note: Updates here are also needed in /wonder-blocks-tokens/src/tokens/media-queries.ts
const xs =
"@media screen and (max-width: 567px) /* breakpoint.mediaQuery.xs */";
const sm =
"@media screen and (min-width: 568px) and (max-width: 681px) /* breakpoint.mediaQuery.sm */";
Comment on lines +5 to +8

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should probably be a comment here telling people if they make changes here they also need to update tokens/media-queries.ts.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call!

const md =
"@media screen and (min-width: 682px) and (max-width: 1023px) /* breakpoint.mediaQuery.md */";
const lg =
"@media screen and (min-width: 682px) and (max-width: 1024px) /* breakpoint.mediaQuery.lg */";
const xl =
"@media screen and (min-width: 1024px) /* breakpoint.mediaQuery.xl */";
const xsOrSmaller =
"@media screen and (max-width: 567px) /* breakpoint.mediaQuery.xsOrSmaller */";
const smOrSmaller =
"@media screen and (max-width: 681px) /* breakpoint.mediaQuery.smOrSmaller */";
const mdOrSmaller =
"@media screen and (max-width: 1023px) /* breakpoint.mediaQuery.mdOrSmaller */";
const lgOrSmaller =
"@media screen and (min-width: 1024px) /* breakpoint.mediaQuery.lgOrLarger */";
const smOrLarger =
"@media screen and (min-width: 568px) /* breakpoint.mediaQuery.smOrLarger */";
const mdOrLarger =
"@media screen and (min-width: 682px) /* breakpoint.mediaQuery.mdOrLarger */";
const lgOrLarger =
"@media screen and (min-width: 1024px) /* breakpoint.mediaQuery.lgOrLarger */";

type _CSSProperties = React.CSSProperties & {
/**
* Browser Specific
Expand All @@ -17,6 +43,19 @@ declare module "aphrodite" {
"@media (max-width: 1023px)"?: React.CSSProperties;
"@media (min-width: 1024px)"?: React.CSSProperties;
"@media (min-width: 1168px)"?: React.CSSProperties;

[xs]?: React.CSSProperties;
[sm]?: React.CSSProperties;
[md]?: React.CSSProperties;
[lg]?: React.CSSProperties;
[xl]?: React.CSSProperties;
[xsOrSmaller]?: React.CSSProperties;
[smOrSmaller]?: React.CSSProperties;
[mdOrSmaller]?: React.CSSProperties;
[lgOrSmaller]?: React.CSSProperties;
[smOrLarger]?: React.CSSProperties;
[mdOrLarger]?: React.CSSProperties;
[lgOrLarger]?: React.CSSProperties;
};

/**
Expand Down