Skip to content

Commit

Permalink
feat: support aspect ratio token (#1979)
Browse files Browse the repository at this point in the history
* feat: add aspect ratio token

* test: update snaps

* docs: update aspect ratio

* docs: update changeset
  • Loading branch information
segunadebayo authored Jan 11, 2024
1 parent 24e9766 commit bee3ec8
Show file tree
Hide file tree
Showing 18 changed files with 163 additions and 18 deletions.
45 changes: 45 additions & 0 deletions .changeset/polite-buttons-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
'@pandacss/token-dictionary': minor
'@pandacss/preset-panda': minor
'@pandacss/preset-base': minor
---

Add support for aspect ratio tokens in the panda config or preset. Aspect ratio tokens are used to define the aspect
ratio of an element.

```js
export default defineConfig({
// ...
theme: {
extend: {
// add aspect ratio tokens
tokens: {
aspectRatios: {
'1:1': '1',
'16:9': '16/9',
},
},
},
},
})
```

Here's what the default aspect ratio tokens in the base preset looks like:

```json
{
"square": { "value": "1 / 1" },
"landscape": { "value": "4 / 3" },
"portrait": { "value": "3 / 4" },
"wide": { "value": "16 / 9" },
"ultrawide": { "value": "18 / 5" },
"golden": { "value": "1.618 / 1" }
}
```

**Breaking Change**

The built-in token values has been removed from the `aspectRatio` utility to the `@pandacss/preset-base` as a token.

For most users, this change should be a drop-in replacement. However, if you used a custom preset in the config, you
might need to update it to include the new aspect ratio tokens.
10 changes: 8 additions & 2 deletions packages/generator/__tests__/generate-prop-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('generate property types', () => {
import type { Tokens } from '../tokens/index';
interface PropertyValueTypes {
aspectRatio: \\"auto\\" | \\"square\\" | \\"landscape\\" | \\"portrait\\" | \\"wide\\" | \\"ultrawide\\" | \\"golden\\";
aspectRatio: Tokens[\\"aspectRatios\\"];
zIndex: Tokens[\\"zIndex\\"];
top: Tokens[\\"spacing\\"];
left: Tokens[\\"spacing\\"];
Expand Down Expand Up @@ -396,12 +396,15 @@ describe('generate property types', () => {
| 'visibility'
| 'wordBreak'
| 'writingMode'
type WithEscapeHatch<T> = T | \`[\${string}]\`
type FilterVagueString<Key, Value> = Value extends boolean
? Value
: Key extends StrictableProps
? Value extends \`\${infer _}\` ? Value : never
: Value
type PropOrCondition<Key, Value> = ConditionalValue<Value | (string & {})>
type PropertyTypeValue<T extends string> = T extends keyof PropertyTypes
Expand Down Expand Up @@ -430,7 +433,7 @@ describe('generate property types', () => {
import type { Tokens } from '../tokens/index';
interface PropertyValueTypes {
aspectRatio: \\"auto\\" | \\"square\\" | \\"landscape\\" | \\"portrait\\" | \\"wide\\" | \\"ultrawide\\" | \\"golden\\";
aspectRatio: Tokens[\\"aspectRatios\\"];
zIndex: Tokens[\\"zIndex\\"];
top: Tokens[\\"spacing\\"];
left: Tokens[\\"spacing\\"];
Expand Down Expand Up @@ -808,12 +811,15 @@ describe('generate property types', () => {
| 'visibility'
| 'wordBreak'
| 'writingMode'
type WithEscapeHatch<T> = T | \`[\${string}]\`
type FilterVagueString<Key, Value> = Value extends boolean
? Value
: Key extends StrictableProps
? Value extends \`\${infer _}\` ? Value : never
: Value
type PropOrCondition<Key, Value> = ConditionalValue<WithEscapeHatch<Value>>
type PropertyTypeValue<T extends string> = T extends keyof PropertyTypes
Expand Down
7 changes: 5 additions & 2 deletions packages/generator/__tests__/generate-token-dts.test.ts

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions packages/generator/__tests__/generate-token-js.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,30 @@ test('[dts] should generate package', () => {
expect(tokenJs(fixtureDefaults)).toMatchInlineSnapshot(
`
"const tokens = {
\\"aspectRatios.square\\": {
\\"value\\": \\"1 / 1\\",
\\"variable\\": \\"var(--aspect-ratios-square)\\"
},
\\"aspectRatios.landscape\\": {
\\"value\\": \\"4 / 3\\",
\\"variable\\": \\"var(--aspect-ratios-landscape)\\"
},
\\"aspectRatios.portrait\\": {
\\"value\\": \\"3 / 4\\",
\\"variable\\": \\"var(--aspect-ratios-portrait)\\"
},
\\"aspectRatios.wide\\": {
\\"value\\": \\"16 / 9\\",
\\"variable\\": \\"var(--aspect-ratios-wide)\\"
},
\\"aspectRatios.ultrawide\\": {
\\"value\\": \\"18 / 5\\",
\\"variable\\": \\"var(--aspect-ratios-ultrawide)\\"
},
\\"aspectRatios.golden\\": {
\\"value\\": \\"1.618 / 1\\",
\\"variable\\": \\"var(--aspect-ratios-golden)\\"
},
\\"borders.none\\": {
\\"value\\": \\"none\\",
\\"variable\\": \\"var(--borders-none)\\"
Expand Down
6 changes: 6 additions & 0 deletions packages/generator/__tests__/generate-token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ describe('generator', () => {
expect(tokenCss(fixtureDefaults)).toMatchInlineSnapshot(`
"@layer tokens {
:where(html) {
--aspect-ratios-square: 1 / 1;
--aspect-ratios-landscape: 4 / 3;
--aspect-ratios-portrait: 3 / 4;
--aspect-ratios-wide: 16 / 9;
--aspect-ratios-ultrawide: 18 / 5;
--aspect-ratios-golden: 1.618 / 1;
--borders-none: none;
--easings-default: cubic-bezier(0.4, 0, 0.2, 1);
--easings-linear: linear;
Expand Down
3 changes: 3 additions & 0 deletions packages/generator/src/artifacts/types/prop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,15 @@ export function generatePropTypes(ctx: Context) {
| 'visibility'
| 'wordBreak'
| 'writingMode'
type WithEscapeHatch<T> = T | \`[\${string}]\`
type FilterVagueString<Key, Value> = Value extends boolean
? Value
: Key extends StrictableProps
? Value extends \`\${infer _}\` ? Value : never
: Value
type PropOrCondition<Key, Value> = ${match(ctx.config)
.with(
{ strictTokens: true, strictPropertyValues: true },
Expand Down
1 change: 1 addition & 0 deletions packages/generator/src/artifacts/types/token-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { outdent } from 'outdent'
import pluralize from 'pluralize'

const categories = [
'aspectRatios',
'zIndex',
'opacity',
'colors',
Expand Down
10 changes: 1 addition & 9 deletions packages/preset-base/src/utilities/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,7 @@ import type { UtilityConfig } from '@pandacss/types'
export const layout: UtilityConfig = {
aspectRatio: {
className: 'aspect',
values: {
auto: 'auto',
square: '1 / 1',
landscape: '4 / 3',
portrait: '3 / 4',
wide: '16 / 9',
ultrawide: '18 / 5',
golden: '1.618 / 1',
},
values: 'aspectRatios',
},
boxDecorationBreak: {
className: 'decoration',
Expand Down
8 changes: 8 additions & 0 deletions packages/preset-panda/src/aspect-ratios.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const aspectRatios = {
square: { value: '1 / 1' },
landscape: { value: '4 / 3' },
portrait: { value: '3 / 4' },
wide: { value: '16 / 9' },
ultrawide: { value: '18 / 5' },
golden: { value: '1.618 / 1' },
}
4 changes: 3 additions & 1 deletion packages/preset-panda/src/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { Tokens } from '@pandacss/types'
import { aspectRatios } from './aspect-ratios'
import { borders } from './borders'
import { colors } from './colors'
import { animations } from './keyframes'
import { borders } from './borders'
import { shadows } from './shadows'
import { sizes } from './sizes'
import { spacing } from './spacing'
Expand All @@ -10,6 +11,7 @@ import { fonts, fontSizes, fontWeights, letterSpacings, lineHeights } from './ty
const defineTokens = <T extends Tokens>(v: T) => v

export const tokens = defineTokens({
aspectRatios,
borders,
easings: {
default: { value: 'cubic-bezier(0.4, 0, 0.2, 1)' },
Expand Down
24 changes: 24 additions & 0 deletions packages/studio/styled-system/tokens/index.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
const tokens = {
"aspectRatios.square": {
"value": "1 / 1",
"variable": "var(--aspect-ratios-square)"
},
"aspectRatios.landscape": {
"value": "4 / 3",
"variable": "var(--aspect-ratios-landscape)"
},
"aspectRatios.portrait": {
"value": "3 / 4",
"variable": "var(--aspect-ratios-portrait)"
},
"aspectRatios.wide": {
"value": "16 / 9",
"variable": "var(--aspect-ratios-wide)"
},
"aspectRatios.ultrawide": {
"value": "18 / 5",
"variable": "var(--aspect-ratios-ultrawide)"
},
"aspectRatios.golden": {
"value": "1.618 / 1",
"variable": "var(--aspect-ratios-golden)"
},
"borders.none": {
"value": "none",
"variable": "var(--borders-none)"
Expand Down
7 changes: 5 additions & 2 deletions packages/studio/styled-system/tokens/tokens.d.ts

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion packages/studio/styled-system/types/prop-type.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { CssProperties } from './system-types';
import type { Tokens } from '../tokens/index';

interface PropertyValueTypes {
aspectRatio: "auto" | "square" | "landscape" | "portrait" | "wide" | "ultrawide" | "golden";
aspectRatio: Tokens["aspectRatios"];
zIndex: Tokens["zIndex"];
top: Tokens["spacing"];
left: Tokens["spacing"];
Expand Down Expand Up @@ -383,12 +383,15 @@ type StrictableProps =
| 'visibility'
| 'wordBreak'
| 'writingMode'

type WithEscapeHatch<T> = T | `[${string}]`

type FilterVagueString<Key, Value> = Value extends boolean
? Value
: Key extends StrictableProps
? Value extends `${infer _}` ? Value : never
: Value

type PropOrCondition<Key, Value> = ConditionalValue<Value | (string & {})>

type PropertyTypeValue<T extends string> = T extends keyof PropertyTypes
Expand Down
8 changes: 8 additions & 0 deletions packages/token-dictionary/__tests__/tokens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,14 @@ test('should work with default fixture', () => {

expect(formats.getFlattenedValues(dictionary)).toMatchInlineSnapshot(`
Map {
"aspectRatios" => Map {
"square" => "var(--aspect-ratios-square)",
"landscape" => "var(--aspect-ratios-landscape)",
"portrait" => "var(--aspect-ratios-portrait)",
"wide" => "var(--aspect-ratios-wide)",
"ultrawide" => "var(--aspect-ratios-ultrawide)",
"golden" => "var(--aspect-ratios-golden)",
},
"borders" => Map {
"none" => "var(--borders-none)",
},
Expand Down
1 change: 1 addition & 0 deletions packages/token-dictionary/src/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,5 @@ const TOKEN_TYPES = {
borders: 'border',
components: 'cti',
assets: 'asset',
aspectRatios: 'aspectRatio',
}
1 change: 1 addition & 0 deletions packages/types/src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export interface TokenDataTypes {
gradients: string | Gradient
assets: string | Asset
borderWidths: string
aspectRatios: string
}

export type Tokens = {
Expand Down
15 changes: 15 additions & 0 deletions website/pages/docs/theming/tokens.md
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,21 @@ const theme = {

> Animation tokens are typically used in `animation` property.
### Aspect Ratios

Aspect ratio tokens represent the aspect ratio of an element. Its value is defined as a string.

```jsx
const theme = {
tokens: {
aspectRatios: {
'1:1': { value: '1 / 1' },
'16:9': { value: '16 / 9' }
}
}
}
```

## Token Helpers

To help defining tokens in a type-safe way, you can use the tokens [Config Functions](/docs/customization/config-functions#token-creators).
2 changes: 1 addition & 1 deletion website/pages/docs/utilities/layout.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Panda provides style properties for styling layout of an element

Use the `aspectRatio` utilities to set the desired aspect ratio of an element.

Values can be `square`, `portrait`, `landscape`, `wide`, `ultrawide`, `golden` or a number.
Values can reference the `aspectRatios` token category.

```jsx
<div className={css({ aspectRatio: 'square' })} />
Expand Down

3 comments on commit bee3ec8

@vercel
Copy link

@vercel vercel bot commented on bee3ec8 Jan 11, 2024

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

panda-docs – ./website

panda-docs-git-main-chakra-ui.vercel.app
panda-css.com
panda-docs-chakra-ui.vercel.app
panda-docs.vercel.app

@vercel
Copy link

@vercel vercel bot commented on bee3ec8 Jan 11, 2024

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

panda-studio – ./

panda-app.vercel.app
panda-studio-chakra-ui.vercel.app
panda-studio-git-main-chakra-ui.vercel.app

@vercel
Copy link

@vercel vercel bot commented on bee3ec8 Jan 11, 2024

Choose a reason for hiding this comment

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

Please sign in to comment.