@sentry/scraps 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +105 -0
- package/README.md +67 -0
- package/dist/code/inlineCode.d.ts +7 -0
- package/dist/code/inlineCode.js +38 -0
- package/dist/cssTypes.d.ts +57 -0
- package/dist/cssTypes.js +0 -0
- package/dist/hotkey/kbd.d.ts +8 -0
- package/dist/hotkey/kbd.js +31 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +9 -0
- package/dist/layout/container.d.ts +114 -0
- package/dist/layout/container.js +161 -0
- package/dist/layout/flex.d.ts +42 -0
- package/dist/layout/flex.js +42 -0
- package/dist/layout/grid.d.ts +65 -0
- package/dist/layout/grid.js +83 -0
- package/dist/layout/index.d.ts +13 -0
- package/dist/layout/index.js +6 -0
- package/dist/layout/stack.d.ts +15 -0
- package/dist/layout/stack.js +55 -0
- package/dist/layout/styles.d.ts +118 -0
- package/dist/layout/styles.js +237 -0
- package/dist/layout/surface.d.ts +27 -0
- package/dist/layout/surface.js +28 -0
- package/dist/loader/indeterminateLoader.d.ts +5 -0
- package/dist/loader/indeterminateLoader.js +122 -0
- package/dist/loader/index.d.ts +1 -0
- package/dist/loader/index.js +1 -0
- package/dist/quote/index.d.ts +1 -0
- package/dist/quote/index.js +1 -0
- package/dist/quote/quote.d.ts +13 -0
- package/dist/quote/quote.js +73 -0
- package/dist/separator/index.d.ts +1 -0
- package/dist/separator/index.js +1 -0
- package/dist/separator/separator.d.ts +15 -0
- package/dist/separator/separator.js +29 -0
- package/dist/text/heading.d.ts +44 -0
- package/dist/text/heading.js +69 -0
- package/dist/text/index.d.ts +2 -0
- package/dist/text/index.js +2 -0
- package/dist/text/styles.d.ts +5 -0
- package/dist/text/styles.js +18 -0
- package/dist/text/text.d.ts +152 -0
- package/dist/text/text.js +81 -0
- package/dist/theme/base.d.ts +106 -0
- package/dist/theme/base.js +7 -0
- package/dist/theme/dark.d.ts +1904 -0
- package/dist/theme/dark.js +430 -0
- package/dist/theme/index.d.ts +14 -0
- package/dist/theme/index.js +3 -0
- package/dist/theme/light.d.ts +1904 -0
- package/dist/theme/light.js +430 -0
- package/dist/theme/types.d.ts +80 -0
- package/dist/theme/types.js +0 -0
- package/dist/tokens/color.d.ts +3309 -0
- package/dist/tokens/color.js +453 -0
- package/dist/tokens/index.d.ts +3 -0
- package/dist/tokens/index.js +3 -0
- package/dist/tokens/size.d.ts +372 -0
- package/dist/tokens/size.js +75 -0
- package/dist/tokens/typography.d.ts +88 -0
- package/dist/tokens/typography.js +35 -0
- package/package.json +83 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import styled from "@emotion/styled";
|
|
2
|
+
import { Container } from "./container.js";
|
|
3
|
+
import { FLEX_JUSTIFY_CONTENT, getSpacing, rc } from "./styles.js";
|
|
4
|
+
const omitFlexProps = new Set([
|
|
5
|
+
'as',
|
|
6
|
+
'direction',
|
|
7
|
+
'flex',
|
|
8
|
+
'gap',
|
|
9
|
+
'display',
|
|
10
|
+
'align',
|
|
11
|
+
'justify',
|
|
12
|
+
'wrap'
|
|
13
|
+
]);
|
|
14
|
+
const Flex = styled(Container, {
|
|
15
|
+
shouldForwardProp: (prop)=>!omitFlexProps.has(prop)
|
|
16
|
+
})`
|
|
17
|
+
${(p)=>rc('display', p.display ?? 'flex', p.theme)};
|
|
18
|
+
${(p)=>rc('order', p.order, p.theme)};
|
|
19
|
+
${(p)=>rc('gap', p.gap, p.theme, getSpacing)};
|
|
20
|
+
|
|
21
|
+
${(p)=>rc('flex-direction', p.direction, p.theme)};
|
|
22
|
+
${(p)=>rc('flex-wrap', p.wrap, p.theme)};
|
|
23
|
+
${(p)=>rc('flex', p.flex, p.theme)};
|
|
24
|
+
${(p)=>rc('justify-content', p.justify, p.theme, (value)=>void 0 === value ? void 0 : FLEX_JUSTIFY_CONTENT[value])};
|
|
25
|
+
|
|
26
|
+
${(p)=>rc('align-items', p.align, p.theme, (value, _breakpoint, _theme)=>{
|
|
27
|
+
switch(value){
|
|
28
|
+
case 'start':
|
|
29
|
+
return 'flex-start';
|
|
30
|
+
case 'end':
|
|
31
|
+
return 'flex-end';
|
|
32
|
+
default:
|
|
33
|
+
return value;
|
|
34
|
+
}
|
|
35
|
+
})};
|
|
36
|
+
/**
|
|
37
|
+
* This cast is required because styled-components does not preserve the generic signature of the wrapped component.
|
|
38
|
+
* By default, the generic type parameter <T> is lost, so we use 'as unknown as' to restore the correct typing.
|
|
39
|
+
* https://github.com/styled-components/styled-components/issues/1803
|
|
40
|
+
*/
|
|
41
|
+
`;
|
|
42
|
+
export { Flex };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import type { CSS } from '../cssTypes.js';
|
|
2
|
+
import type { SpaceSize } from '../theme/index.js';
|
|
3
|
+
import { type ContainerElement, type ContainerProps, type ContainerPropsWithRenderFunction } from './container.js';
|
|
4
|
+
import { type Responsive } from './styles.js';
|
|
5
|
+
interface GridLayoutProps {
|
|
6
|
+
/**
|
|
7
|
+
* Aligns grid items along the column axis within their grid cells.
|
|
8
|
+
* Uses CSS align-items property.
|
|
9
|
+
*/
|
|
10
|
+
align?: Responsive<'start' | 'end' | 'center' | 'baseline' | 'stretch'>;
|
|
11
|
+
/**
|
|
12
|
+
* Aligns grid items along the column axis within their grid cells.
|
|
13
|
+
* Uses CSS align-content property.
|
|
14
|
+
*/
|
|
15
|
+
alignContent?: Responsive<'start' | 'end' | 'center' | 'between' | 'around' | 'evenly' | 'stretch'>;
|
|
16
|
+
/**
|
|
17
|
+
* Defines named grid areas for child components to reference.
|
|
18
|
+
* Uses CSS grid-template-areas property.
|
|
19
|
+
*/
|
|
20
|
+
areas?: Responsive<CSS['gridTemplateAreas']>;
|
|
21
|
+
/**
|
|
22
|
+
* Specifies the size of auto-generated column tracks.
|
|
23
|
+
* Uses CSS grid-auto-columns property.
|
|
24
|
+
*/
|
|
25
|
+
autoColumns?: Responsive<CSS['gridAutoColumns']>;
|
|
26
|
+
/**
|
|
27
|
+
* Specifies the size of auto-generated row tracks.
|
|
28
|
+
* Uses CSS grid-auto-rows property.
|
|
29
|
+
*/
|
|
30
|
+
autoRows?: Responsive<CSS['gridAutoRows']>;
|
|
31
|
+
/**
|
|
32
|
+
* Defines the column tracks of the grid.
|
|
33
|
+
* Uses CSS grid-template-columns property.
|
|
34
|
+
*/
|
|
35
|
+
columns?: Responsive<CSS['gridTemplateColumns']>;
|
|
36
|
+
/**
|
|
37
|
+
* Determines the grid display type.
|
|
38
|
+
*/
|
|
39
|
+
display?: Responsive<'grid' | 'inline-grid' | 'none'>;
|
|
40
|
+
/**
|
|
41
|
+
* Controls the auto-placement algorithm for grid items.
|
|
42
|
+
* Uses CSS grid-auto-flow property.
|
|
43
|
+
*/
|
|
44
|
+
flow?: Responsive<'row' | 'column' | 'row dense' | 'column dense'>;
|
|
45
|
+
gap?: Responsive<SpaceSize | `${SpaceSize} ${SpaceSize}`>;
|
|
46
|
+
/**
|
|
47
|
+
* Aligns the grid container's content along the row axis when the grid is smaller than its container.
|
|
48
|
+
* Uses CSS justify-content property.
|
|
49
|
+
*/
|
|
50
|
+
justify?: Responsive<'start' | 'end' | 'center' | 'between' | 'around' | 'evenly' | 'stretch'>;
|
|
51
|
+
/**
|
|
52
|
+
* Aligns grid items along the row axis within their grid cells.
|
|
53
|
+
* Uses CSS justify-items property.
|
|
54
|
+
*/
|
|
55
|
+
justifyItems?: Responsive<'start' | 'end' | 'center' | 'stretch'>;
|
|
56
|
+
/**
|
|
57
|
+
* Defines the row tracks of the grid.
|
|
58
|
+
* Uses CSS grid-template-rows property.
|
|
59
|
+
*/
|
|
60
|
+
rows?: Responsive<CSS['gridTemplateRows']>;
|
|
61
|
+
}
|
|
62
|
+
export type GridProps<T extends ContainerElement = 'div'> = ContainerProps<T> & GridLayoutProps;
|
|
63
|
+
export type GridPropsWithRenderFunction<T extends ContainerElement = 'div'> = ContainerPropsWithRenderFunction<T> & GridLayoutProps;
|
|
64
|
+
export declare const Grid: <T extends ContainerElement = "div">(props: GridProps<T> | GridPropsWithRenderFunction<T>) => React.ReactElement;
|
|
65
|
+
export {};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import styled from "@emotion/styled";
|
|
2
|
+
import { Container } from "./container.js";
|
|
3
|
+
import { getSpacing, rc } from "./styles.js";
|
|
4
|
+
const omitGridProps = new Set([
|
|
5
|
+
'align',
|
|
6
|
+
'alignContent',
|
|
7
|
+
'as',
|
|
8
|
+
'autoColumns',
|
|
9
|
+
'autoRows',
|
|
10
|
+
'flow',
|
|
11
|
+
'gap',
|
|
12
|
+
'display',
|
|
13
|
+
'justify',
|
|
14
|
+
'justifyItems',
|
|
15
|
+
'areas',
|
|
16
|
+
'columns',
|
|
17
|
+
'rows'
|
|
18
|
+
]);
|
|
19
|
+
const Grid = styled(Container, {
|
|
20
|
+
shouldForwardProp: (prop)=>!omitGridProps.has(prop)
|
|
21
|
+
})`
|
|
22
|
+
${(p)=>rc('display', p.display ?? 'grid', p.theme)}
|
|
23
|
+
|
|
24
|
+
${(p)=>rc('gap', p.gap, p.theme, getSpacing)};
|
|
25
|
+
|
|
26
|
+
${(p)=>rc('grid-template-columns', p.columns, p.theme)};
|
|
27
|
+
${(p)=>rc('grid-template-rows', p.rows, p.theme)};
|
|
28
|
+
${(p)=>rc('grid-template-areas', p.areas, p.theme)};
|
|
29
|
+
${(p)=>rc('grid-auto-columns', p.autoColumns, p.theme)};
|
|
30
|
+
${(p)=>rc('grid-auto-rows', p.autoRows, p.theme)};
|
|
31
|
+
${(p)=>rc('grid-auto-flow', p.flow, p.theme)};
|
|
32
|
+
|
|
33
|
+
${(p)=>rc('justify-content', p.justify, p.theme, (value, _breakpoint, _theme)=>{
|
|
34
|
+
switch(value){
|
|
35
|
+
case 'start':
|
|
36
|
+
return 'start';
|
|
37
|
+
case 'end':
|
|
38
|
+
return 'end';
|
|
39
|
+
case 'center':
|
|
40
|
+
return 'center';
|
|
41
|
+
case 'between':
|
|
42
|
+
return 'space-between';
|
|
43
|
+
case 'around':
|
|
44
|
+
return 'space-around';
|
|
45
|
+
case 'evenly':
|
|
46
|
+
return 'space-evenly';
|
|
47
|
+
case 'stretch':
|
|
48
|
+
return 'stretch';
|
|
49
|
+
default:
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
})};
|
|
53
|
+
|
|
54
|
+
${(p)=>rc('align-content', p.alignContent, p.theme, (value, _breakpoint, _theme)=>{
|
|
55
|
+
switch(value){
|
|
56
|
+
case 'start':
|
|
57
|
+
return 'start';
|
|
58
|
+
case 'end':
|
|
59
|
+
return 'end';
|
|
60
|
+
case 'center':
|
|
61
|
+
return 'center';
|
|
62
|
+
case 'between':
|
|
63
|
+
return 'space-between';
|
|
64
|
+
case 'around':
|
|
65
|
+
return 'space-around';
|
|
66
|
+
case 'evenly':
|
|
67
|
+
return 'space-evenly';
|
|
68
|
+
case 'stretch':
|
|
69
|
+
return 'stretch';
|
|
70
|
+
default:
|
|
71
|
+
return value;
|
|
72
|
+
}
|
|
73
|
+
})};
|
|
74
|
+
|
|
75
|
+
${(p)=>rc('align-items', p.align, p.theme)};
|
|
76
|
+
${(p)=>rc('justify-items', p.justifyItems, p.theme)};
|
|
77
|
+
/**
|
|
78
|
+
* This cast is required because styled-components does not preserve the generic signature of the wrapped component.
|
|
79
|
+
* By default, the generic type parameter <T> is lost, so we use 'as unknown as' to restore the correct typing.
|
|
80
|
+
* https://github.com/styled-components/styled-components/issues/1803
|
|
81
|
+
*/
|
|
82
|
+
`;
|
|
83
|
+
export { Grid };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export { Container, type ContainerProps, type ContainerPropsWithRenderFunction, } from './container.js';
|
|
2
|
+
export { Surface } from './surface.js';
|
|
3
|
+
export { Flex, type FlexProps } from './flex.js';
|
|
4
|
+
export { Grid, type GridProps } from './grid.js';
|
|
5
|
+
export { Stack, type StackProps } from './stack.js';
|
|
6
|
+
/**
|
|
7
|
+
* @TODO: these should probably be private to scraps
|
|
8
|
+
*/
|
|
9
|
+
export { ContainerQueryProvider, rc, type Responsive, type ResponsiveKey,
|
|
10
|
+
/** @public */
|
|
11
|
+
useContainerBreakpoint, useHasContainerQuery, useResponsivePropResolver, useResponsivePropValue, } from './styles.js';
|
|
12
|
+
export { getBorder, getMargin, getRadius, getSpacing } from './styles.js';
|
|
13
|
+
export { FLEX_JUSTIFY_CONTENT, type FlexJustify } from './styles.js';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { Container } from "./container.js";
|
|
2
|
+
export { Surface } from "./surface.js";
|
|
3
|
+
export { Flex } from "./flex.js";
|
|
4
|
+
export { Grid } from "./grid.js";
|
|
5
|
+
export { Stack } from "./stack.js";
|
|
6
|
+
export { ContainerQueryProvider, FLEX_JUSTIFY_CONTENT, getBorder, getMargin, getRadius, getSpacing, rc, useContainerBreakpoint, useHasContainerQuery, useResponsivePropResolver, useResponsivePropValue } from "./styles.js";
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type SeparatorProps } from '../separator/index.js';
|
|
2
|
+
import type { ContainerElement } from './container.js';
|
|
3
|
+
import { type FlexProps, type FlexPropsWithRenderFunction } from './flex.js';
|
|
4
|
+
/**
|
|
5
|
+
* Stack is just a super set of Flex props with a default direction initializer to 'column'.
|
|
6
|
+
*/
|
|
7
|
+
export type StackProps<T extends ContainerElement = 'div'> = FlexProps<T>;
|
|
8
|
+
export type StackPropsWithRenderFunction<T extends ContainerElement = 'div'> = FlexPropsWithRenderFunction<T>;
|
|
9
|
+
type StackSeparatorProps = Omit<SeparatorProps, 'orientation'>;
|
|
10
|
+
export declare const Stack: (<T extends ContainerElement = "div">(props: StackProps<T> | StackPropsWithRenderFunction<T>) => React.ReactElement) & {
|
|
11
|
+
Separator: import("@emotion/styled").StyledComponent<StackSeparatorProps & {
|
|
12
|
+
theme?: import("@emotion/react").Theme;
|
|
13
|
+
}, {}, {}>;
|
|
14
|
+
};
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { jsx } from "@emotion/react/jsx-runtime";
|
|
2
|
+
import { createContext, useContext, useMemo } from "react";
|
|
3
|
+
import styled from "@emotion/styled";
|
|
4
|
+
import { Separator } from "../separator/index.js";
|
|
5
|
+
import { Flex } from "./flex.js";
|
|
6
|
+
import { useResponsivePropValue } from "./styles.js";
|
|
7
|
+
const StackComponent = styled(({ direction = 'column', ...props })=>{
|
|
8
|
+
const directionContext = useMemo(()=>({
|
|
9
|
+
direction
|
|
10
|
+
}), [
|
|
11
|
+
direction
|
|
12
|
+
]);
|
|
13
|
+
return /*#__PURE__*/ jsx(StackDirectionContext.Provider, {
|
|
14
|
+
value: directionContext,
|
|
15
|
+
children: /*#__PURE__*/ jsx(Flex, {
|
|
16
|
+
...props,
|
|
17
|
+
direction: direction
|
|
18
|
+
})
|
|
19
|
+
});
|
|
20
|
+
})`
|
|
21
|
+
/**
|
|
22
|
+
* This cast is required because styled-components does not preserve the generic signature of the wrapped component.
|
|
23
|
+
* By default, the generic type parameter <T> is lost, so we use 'as unknown as' to restore the correct typing.
|
|
24
|
+
* https://github.com/styled-components/styled-components/issues/1803
|
|
25
|
+
*/
|
|
26
|
+
`;
|
|
27
|
+
function getOrientationFromDirection(direction) {
|
|
28
|
+
switch(direction){
|
|
29
|
+
case 'row':
|
|
30
|
+
case 'row-reverse':
|
|
31
|
+
return 'horizontal';
|
|
32
|
+
case 'column':
|
|
33
|
+
case 'column-reverse':
|
|
34
|
+
return 'vertical';
|
|
35
|
+
default:
|
|
36
|
+
throw new TypeError('No Stack Direction was provided');
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const StackDirectionContext = /*#__PURE__*/ createContext({
|
|
40
|
+
direction: 'row'
|
|
41
|
+
});
|
|
42
|
+
const StackSeparator = styled((props)=>{
|
|
43
|
+
const { direction } = useContext(StackDirectionContext);
|
|
44
|
+
const responsiveDirection = useResponsivePropValue(direction);
|
|
45
|
+
const orientation = getOrientationFromDirection(responsiveDirection);
|
|
46
|
+
return /*#__PURE__*/ jsx(Separator, {
|
|
47
|
+
...props,
|
|
48
|
+
orientation: 'horizontal' === orientation ? 'vertical' : 'horizontal',
|
|
49
|
+
border: props.border ?? 'primary'
|
|
50
|
+
});
|
|
51
|
+
})``;
|
|
52
|
+
const Stack = Object.assign(StackComponent, {
|
|
53
|
+
Separator: StackSeparator
|
|
54
|
+
});
|
|
55
|
+
export { Stack };
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { ReactNode, RefObject } from 'react';
|
|
2
|
+
import type { BorderVariant, BreakpointSize, ContainerBreakpointSize, RadiusSize, SpaceSize, Theme } from '../theme/index.js';
|
|
3
|
+
type ResponsiveBreakpoint = BreakpointSize | ContainerBreakpointSize;
|
|
4
|
+
export declare function rc<T>(property: string, value: Responsive<T> | undefined, theme: Theme, resolver?: (value: T | undefined, breakpoint: ResponsiveBreakpoint | undefined, theme: Theme) => string | undefined): string | undefined;
|
|
5
|
+
type Margin = SpaceSize | 'auto' | '0';
|
|
6
|
+
/**
|
|
7
|
+
* The `justify` vocabulary a flex container accepts, and its mapping onto CSS
|
|
8
|
+
* `justify-content`. Shared so components that are not themselves a `Flex` but
|
|
9
|
+
* lay their content out with one — table header cells, for instance — can take
|
|
10
|
+
* the same prop values a `Flex` does. `Grid` deliberately keeps its own map:
|
|
11
|
+
* grid alignment resolves `start`/`end` to the grid keywords, not the flex ones.
|
|
12
|
+
*/
|
|
13
|
+
export type FlexJustify = 'start' | 'end' | 'center' | 'between' | 'around' | 'evenly' | 'left' | 'right';
|
|
14
|
+
export declare const FLEX_JUSTIFY_CONTENT: {
|
|
15
|
+
readonly around: "space-around";
|
|
16
|
+
readonly between: "space-between";
|
|
17
|
+
readonly center: "center";
|
|
18
|
+
readonly end: "flex-end";
|
|
19
|
+
readonly evenly: "space-evenly";
|
|
20
|
+
readonly left: "left";
|
|
21
|
+
readonly right: "right";
|
|
22
|
+
readonly start: "flex-start";
|
|
23
|
+
};
|
|
24
|
+
export type Shorthand<T extends string, N extends 4 | 2> = N extends 4 ? `${T} ${T} ${T} ${T}` | `${T} ${T} ${T}` | `${T} ${T}` | `${T}` : N extends 2 ? `${T} ${T}` | `${T}` : never;
|
|
25
|
+
/**
|
|
26
|
+
* Responsive prop keys come in two flavors, drawn from two independent scales,
|
|
27
|
+
* and may be combined on one prop:
|
|
28
|
+
* - bare keys (`zero`, `md`, ...) resolve against the nearest query container
|
|
29
|
+
* (`@container`, `theme.container`). Container queries are the default, so
|
|
30
|
+
* they take no prefix.
|
|
31
|
+
* - `screen:`-prefixed keys (`screen:md`, ...) resolve against the viewport
|
|
32
|
+
* (`@media`, `theme.breakpoints`).
|
|
33
|
+
*
|
|
34
|
+
* e.g. `direction={{zero: 'column', 'screen:lg': 'row'}}` is column until its
|
|
35
|
+
* container reaches the next breakpoint, then a row once the viewport reaches
|
|
36
|
+
* `lg`.
|
|
37
|
+
*/
|
|
38
|
+
type ScreenBreakpoint = `screen:${BreakpointSize}`;
|
|
39
|
+
export type ResponsiveKey = ContainerBreakpointSize | ScreenBreakpoint;
|
|
40
|
+
export type Responsive<T> = T | Partial<Record<ResponsiveKey, T>>;
|
|
41
|
+
export declare function getBorder(border: BorderVariant | undefined, _breakpoint: ResponsiveBreakpoint | undefined, theme: Theme): string | undefined;
|
|
42
|
+
export declare function getRadius(radius: Shorthand<RadiusSize, 4> | undefined, _breakpoint: ResponsiveBreakpoint | undefined, theme: Theme): string | undefined;
|
|
43
|
+
export declare function getSpacing(spacing: Shorthand<SpaceSize, 4> | undefined, _breakpoint: ResponsiveBreakpoint | undefined, theme: Theme): string | undefined;
|
|
44
|
+
export declare function getMargin(margin: Shorthand<Margin, 4> | undefined, _breakpoint: ResponsiveBreakpoint | undefined, theme: Theme): string | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* Resolves a `Responsive<T>` prop to its current value in JS, across both the
|
|
47
|
+
* container and viewport axes — the JS mirror of what `rc()` emits as CSS.
|
|
48
|
+
*
|
|
49
|
+
* This is a low-level building block for **component authors** who accept a
|
|
50
|
+
* `Responsive<T>` prop and must resolve it in JS rather than via CSS (e.g.
|
|
51
|
+
* `Stack`'s `direction`, `SplitPanel`'s `orientation`). Most code shouldn't need
|
|
52
|
+
* it: use plain responsive props (resolved in CSS) for styling, or
|
|
53
|
+
* {@link useContainerBreakpoint} when you need the container's active breakpoint
|
|
54
|
+
* to branch logic. Prefer those unless you're building a responsive prop of your
|
|
55
|
+
* own.
|
|
56
|
+
*/
|
|
57
|
+
export declare function useResponsivePropValue<T>(prop: Responsive<T>): T;
|
|
58
|
+
/**
|
|
59
|
+
* The multi-prop form of {@link useResponsivePropValue}: resolves the current
|
|
60
|
+
* breakpoints once and hands back a resolver, for components that resolve a
|
|
61
|
+
* variable number of `Responsive<T>` values (e.g. one per table column) and so
|
|
62
|
+
* cannot call a hook per value.
|
|
63
|
+
*/
|
|
64
|
+
export declare function useResponsivePropResolver(): <T>(prop: Responsive<T>) => T;
|
|
65
|
+
export declare function useActiveBreakpoint(): BreakpointSize;
|
|
66
|
+
/**
|
|
67
|
+
* Holds the active breakpoint of the nearest ancestor query container, or null
|
|
68
|
+
* when there is no container ancestor. Provided by container elements (those
|
|
69
|
+
* with a `containerType`) so JS-resolved container queries
|
|
70
|
+
* (`useResponsivePropValue`, `useContainerBreakpoint`) can resolve against the
|
|
71
|
+
* container instead of the viewport.
|
|
72
|
+
*
|
|
73
|
+
* We broadcast the already-resolved breakpoint (not the raw inline-size) so the
|
|
74
|
+
* context value only changes when a breakpoint boundary is crossed — consumers
|
|
75
|
+
* aren't re-rendered on every pixel of a resize.
|
|
76
|
+
*
|
|
77
|
+
* CSS-only responsive props don't need this — they resolve natively via
|
|
78
|
+
* `@container` queries. This context exists purely for the JS resolution path.
|
|
79
|
+
*/
|
|
80
|
+
export declare const ContainerQueryContext: import("react").Context<"2xs" | "xs" | "sm" | "md" | "lg" | "xl" | "2xl" | "3xl" | "4xl" | "zero" | "3xs" | "5xl" | null>;
|
|
81
|
+
/**
|
|
82
|
+
* The JS equivalent of a CSS container query: returns the active breakpoint of
|
|
83
|
+
* the nearest ancestor query container (read from `ContainerQueryContext`),
|
|
84
|
+
* mirroring the mobile-first behavior of `rc()`/`@container`. Must be called
|
|
85
|
+
* inside a query container (a `Container`/`Flex`/… with `containerType`); with
|
|
86
|
+
* no container ancestor it resolves to `zero` (the base, matching CSS's plain
|
|
87
|
+
* base declaration).
|
|
88
|
+
*
|
|
89
|
+
* The returned key is on the container scale (`theme.container`: `zero`, `3xs`,
|
|
90
|
+
* …, `5xl`) — distinct from the viewport scale used by `useActiveBreakpoint`.
|
|
91
|
+
*
|
|
92
|
+
* Prefer CSS responsive props (bare breakpoint keys like `{md: …}`) when
|
|
93
|
+
* possible; reach for this hook only when you genuinely need the resolved
|
|
94
|
+
* breakpoint in JS (e.g. to branch rendering). It replaces width-based
|
|
95
|
+
* `useMedia` usage.
|
|
96
|
+
*
|
|
97
|
+
* Returns the active breakpoint *key* so you can branch on it. To resolve a
|
|
98
|
+
* `Responsive<T>` prop to its current *value* in JS (when building a responsive
|
|
99
|
+
* prop of your own), use {@link useResponsivePropValue} instead.
|
|
100
|
+
* @public
|
|
101
|
+
*/
|
|
102
|
+
export declare function useContainerBreakpoint(): ContainerBreakpointSize;
|
|
103
|
+
/**
|
|
104
|
+
* Whether the current element is inside a query container. This is useful for
|
|
105
|
+
* components that can either establish their own container or use an existing
|
|
106
|
+
* one to avoid introducing size containment into an intrinsic-size layout.
|
|
107
|
+
*/
|
|
108
|
+
export declare function useHasContainerQuery(): boolean;
|
|
109
|
+
/**
|
|
110
|
+
* Measures the given element, resolves its active breakpoint, and broadcasts it
|
|
111
|
+
* through ContainerQueryContext. Rendered by container elements so descendants
|
|
112
|
+
* can resolve container-mode responsive props in JS.
|
|
113
|
+
*/
|
|
114
|
+
export declare function ContainerQueryProvider({ elementRef, children, }: {
|
|
115
|
+
children: ReactNode;
|
|
116
|
+
elementRef: RefObject<Element | null>;
|
|
117
|
+
}): import("@emotion/react/jsx-runtime").JSX.Element;
|
|
118
|
+
export {};
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import { jsx } from "@emotion/react/jsx-runtime";
|
|
2
|
+
import { createContext, useCallback, useContext, useLayoutEffect, useMemo, useState, useSyncExternalStore } from "react";
|
|
3
|
+
import { useTheme } from "@emotion/react";
|
|
4
|
+
function rc(property, value, theme, resolver) {
|
|
5
|
+
if (!isResponsive(value)) {
|
|
6
|
+
const resolvedValue = resolver ? resolver(value, void 0, theme) : value;
|
|
7
|
+
if (void 0 === resolvedValue) return;
|
|
8
|
+
return `${property}: ${resolvedValue};`;
|
|
9
|
+
}
|
|
10
|
+
let first = true;
|
|
11
|
+
const declarations = [];
|
|
12
|
+
const emit = (propKey, breakpoint, atRule, size)=>{
|
|
13
|
+
const v = value[propKey];
|
|
14
|
+
const resolvedValue = resolver ? resolver(v, breakpoint, theme) : v;
|
|
15
|
+
if (void 0 === resolvedValue) return;
|
|
16
|
+
if (first) {
|
|
17
|
+
first = false;
|
|
18
|
+
declarations.push(`${property}: ${resolvedValue};`);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
declarations.push(`${atRule} (min-width: ${size}) {
|
|
22
|
+
${property}: ${resolvedValue};
|
|
23
|
+
}`);
|
|
24
|
+
};
|
|
25
|
+
for (const breakpoint of CONTAINER_ORDER)emit(breakpoint, breakpoint, '@container', theme.container[breakpoint]);
|
|
26
|
+
for (const { key, token } of VIEWPORT_ORDER)emit(key, token, '@media', theme.breakpoints[token]);
|
|
27
|
+
return declarations.join('');
|
|
28
|
+
}
|
|
29
|
+
const CONTAINER_ORDER = [
|
|
30
|
+
'zero',
|
|
31
|
+
'3xs',
|
|
32
|
+
'2xs',
|
|
33
|
+
'xs',
|
|
34
|
+
'sm',
|
|
35
|
+
'md',
|
|
36
|
+
'lg',
|
|
37
|
+
'xl',
|
|
38
|
+
'2xl',
|
|
39
|
+
'3xl',
|
|
40
|
+
'4xl',
|
|
41
|
+
'5xl'
|
|
42
|
+
];
|
|
43
|
+
const VIEWPORT_ORDER = [
|
|
44
|
+
{
|
|
45
|
+
key: 'screen:2xs',
|
|
46
|
+
token: '2xs'
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
key: 'screen:xs',
|
|
50
|
+
token: 'xs'
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
key: 'screen:sm',
|
|
54
|
+
token: 'sm'
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
key: 'screen:md',
|
|
58
|
+
token: 'md'
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
key: 'screen:lg',
|
|
62
|
+
token: 'lg'
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
key: 'screen:xl',
|
|
66
|
+
token: 'xl'
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
key: 'screen:2xl',
|
|
70
|
+
token: '2xl'
|
|
71
|
+
}
|
|
72
|
+
];
|
|
73
|
+
const FLEX_JUSTIFY_CONTENT = {
|
|
74
|
+
around: 'space-around',
|
|
75
|
+
between: 'space-between',
|
|
76
|
+
center: 'center',
|
|
77
|
+
end: 'flex-end',
|
|
78
|
+
evenly: 'space-evenly',
|
|
79
|
+
left: 'left',
|
|
80
|
+
right: 'right',
|
|
81
|
+
start: 'flex-start'
|
|
82
|
+
};
|
|
83
|
+
function isResponsive(prop) {
|
|
84
|
+
return 'object' == typeof prop && null !== prop;
|
|
85
|
+
}
|
|
86
|
+
function resolveRadius(sizeComponent, theme) {
|
|
87
|
+
if (void 0 === sizeComponent) return;
|
|
88
|
+
return theme.radius[sizeComponent];
|
|
89
|
+
}
|
|
90
|
+
function resolveSpacing(sizeComponent, theme) {
|
|
91
|
+
return theme.space[sizeComponent] ?? theme.space['0'];
|
|
92
|
+
}
|
|
93
|
+
function resolveMargin(sizeComponent, theme) {
|
|
94
|
+
if ('auto' === sizeComponent) return 'auto';
|
|
95
|
+
if ('0' === sizeComponent) return '0';
|
|
96
|
+
return theme.space[sizeComponent] ?? theme.space['0'];
|
|
97
|
+
}
|
|
98
|
+
function borderValue(key, theme) {
|
|
99
|
+
if ('primary' === key) return theme.tokens.border[key];
|
|
100
|
+
if ('muted' === key || 'secondary' === key) return theme.tokens.border.secondary;
|
|
101
|
+
return theme.tokens.border[key].vibrant;
|
|
102
|
+
}
|
|
103
|
+
function getBorder(border, _breakpoint, theme) {
|
|
104
|
+
if (void 0 === border) return;
|
|
105
|
+
if ('none' === border) return 'none';
|
|
106
|
+
return border.split(' ').map((b)=>`1px solid ${borderValue(b, theme)}`).join(' ');
|
|
107
|
+
}
|
|
108
|
+
function getRadius(radius, _breakpoint, theme) {
|
|
109
|
+
if (void 0 === radius) return;
|
|
110
|
+
if (radius.length < 3) return resolveRadius(radius, theme);
|
|
111
|
+
return radius.split(' ').map((size)=>resolveRadius(size, theme)).join(' ');
|
|
112
|
+
}
|
|
113
|
+
function getSpacing(spacing, _breakpoint, theme) {
|
|
114
|
+
if (void 0 === spacing) return;
|
|
115
|
+
if (spacing.length < 3) return resolveSpacing(spacing, theme);
|
|
116
|
+
return spacing.split(' ').map((size)=>resolveSpacing(size, theme)).join(' ');
|
|
117
|
+
}
|
|
118
|
+
function getMargin(margin, _breakpoint, theme) {
|
|
119
|
+
if (void 0 === margin) return;
|
|
120
|
+
if (margin.length < 3) return resolveMargin(margin, theme);
|
|
121
|
+
return margin.split(' ').map((size)=>resolveMargin(size, theme)).join(' ');
|
|
122
|
+
}
|
|
123
|
+
function useResponsivePropValue(prop) {
|
|
124
|
+
return useResponsivePropResolver()(prop);
|
|
125
|
+
}
|
|
126
|
+
function useResponsivePropResolver() {
|
|
127
|
+
const viewportBreakpoint = useActiveBreakpoint();
|
|
128
|
+
const containerBreakpoint = useContext(ContainerQueryContext) ?? 'zero';
|
|
129
|
+
return useCallback((prop)=>resolveResponsiveProp(prop, containerBreakpoint, viewportBreakpoint), [
|
|
130
|
+
containerBreakpoint,
|
|
131
|
+
viewportBreakpoint
|
|
132
|
+
]);
|
|
133
|
+
}
|
|
134
|
+
function resolveResponsiveProp(prop, containerBreakpoint, viewportBreakpoint) {
|
|
135
|
+
if (!isResponsive(prop)) return prop;
|
|
136
|
+
if (0 === Object.keys(prop).length) throw new Error('Responsive prop must contain at least one breakpoint');
|
|
137
|
+
const containerIndex = CONTAINER_ORDER.indexOf(containerBreakpoint);
|
|
138
|
+
const viewportIndex = VIEWPORT_ORDER.findIndex((e)=>e.token === viewportBreakpoint);
|
|
139
|
+
let resolved;
|
|
140
|
+
let first = true;
|
|
141
|
+
const cascade = (keys, activeIndex)=>{
|
|
142
|
+
for(let i = 0; i < keys.length; i++){
|
|
143
|
+
const key = keys[i];
|
|
144
|
+
if (void 0 === key) continue;
|
|
145
|
+
const value = prop[key];
|
|
146
|
+
if (void 0 !== value) {
|
|
147
|
+
if (first || activeIndex >= i) resolved = value;
|
|
148
|
+
first = false;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
cascade(CONTAINER_ORDER, containerIndex);
|
|
153
|
+
cascade(VIEWPORT_ORDER.map((e)=>e.key), viewportIndex);
|
|
154
|
+
return resolved;
|
|
155
|
+
}
|
|
156
|
+
function useActiveBreakpoint() {
|
|
157
|
+
const theme = useTheme();
|
|
158
|
+
const mediaQueries = useMemo(()=>{
|
|
159
|
+
if ("u" < typeof window || !window.matchMedia) return [];
|
|
160
|
+
const queries = [];
|
|
161
|
+
for(let i = VIEWPORT_ORDER.length - 1; i >= 0; i--){
|
|
162
|
+
const entry = VIEWPORT_ORDER[i];
|
|
163
|
+
if (void 0 !== entry) queries.push({
|
|
164
|
+
breakpoint: entry.token,
|
|
165
|
+
query: window.matchMedia(`(min-width: ${theme.breakpoints[entry.token]})`)
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
return queries;
|
|
169
|
+
}, [
|
|
170
|
+
theme.breakpoints
|
|
171
|
+
]);
|
|
172
|
+
const subscribe = useCallback((onStoreChange)=>{
|
|
173
|
+
if (!mediaQueries.length) return ()=>{};
|
|
174
|
+
const controller = new AbortController();
|
|
175
|
+
for (const query of mediaQueries)query.query.addEventListener('change', onStoreChange, {
|
|
176
|
+
signal: controller.signal
|
|
177
|
+
});
|
|
178
|
+
return ()=>controller.abort();
|
|
179
|
+
}, [
|
|
180
|
+
mediaQueries
|
|
181
|
+
]);
|
|
182
|
+
return useSyncExternalStore(subscribe, ()=>findLargestBreakpoint(mediaQueries), ()=>'2xs');
|
|
183
|
+
}
|
|
184
|
+
function findLargestBreakpoint(queries) {
|
|
185
|
+
for (const query of queries)if (void 0 !== query) {
|
|
186
|
+
if (query.query.matches) return query.breakpoint;
|
|
187
|
+
}
|
|
188
|
+
return '2xs';
|
|
189
|
+
}
|
|
190
|
+
const ContainerQueryContext = /*#__PURE__*/ createContext(null);
|
|
191
|
+
function useContainerBreakpoint() {
|
|
192
|
+
return useContext(ContainerQueryContext) ?? 'zero';
|
|
193
|
+
}
|
|
194
|
+
function useHasContainerQuery() {
|
|
195
|
+
return null !== useContext(ContainerQueryContext);
|
|
196
|
+
}
|
|
197
|
+
function getContentBoxInlineSize(element) {
|
|
198
|
+
const style = window.getComputedStyle(element);
|
|
199
|
+
const padding = (parseFloat(style.paddingLeft) || 0) + (parseFloat(style.paddingRight) || 0);
|
|
200
|
+
return Math.max(0, element.clientWidth - padding);
|
|
201
|
+
}
|
|
202
|
+
function ContainerQueryProvider({ elementRef, children }) {
|
|
203
|
+
const theme = useTheme();
|
|
204
|
+
const [inlineSize, setInlineSize] = useState(0);
|
|
205
|
+
useLayoutEffect(()=>{
|
|
206
|
+
const element = elementRef.current;
|
|
207
|
+
if (!element) return;
|
|
208
|
+
setInlineSize(getContentBoxInlineSize(element));
|
|
209
|
+
const observer = new ResizeObserver((entries)=>{
|
|
210
|
+
const entry = entries[0];
|
|
211
|
+
if (!entry) return;
|
|
212
|
+
const size = entry.contentBoxSize?.[0]?.inlineSize ?? getContentBoxInlineSize(element);
|
|
213
|
+
setInlineSize(size);
|
|
214
|
+
});
|
|
215
|
+
observer.observe(element);
|
|
216
|
+
return ()=>observer.disconnect();
|
|
217
|
+
}, [
|
|
218
|
+
elementRef
|
|
219
|
+
]);
|
|
220
|
+
const breakpoint = useMemo(()=>{
|
|
221
|
+
for(let i = CONTAINER_ORDER.length - 1; i >= 0; i--){
|
|
222
|
+
const bp = CONTAINER_ORDER[i];
|
|
223
|
+
if (void 0 !== bp) {
|
|
224
|
+
if (inlineSize >= parseInt(theme.container[bp], 10)) return bp;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return 'zero';
|
|
228
|
+
}, [
|
|
229
|
+
inlineSize,
|
|
230
|
+
theme.container
|
|
231
|
+
]);
|
|
232
|
+
return /*#__PURE__*/ jsx(ContainerQueryContext, {
|
|
233
|
+
value: breakpoint,
|
|
234
|
+
children: children
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
export { ContainerQueryContext, ContainerQueryProvider, FLEX_JUSTIFY_CONTENT, getBorder, getMargin, getRadius, getSpacing, rc, useActiveBreakpoint, useContainerBreakpoint, useHasContainerQuery, useResponsivePropResolver, useResponsivePropValue };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type ContainerElement, type ContainerProps } from './container.js';
|
|
2
|
+
import type { SurfaceVariant } from '../theme/index.js';
|
|
3
|
+
interface FlatSurfaceProps<T extends ContainerElement = 'div'> extends Omit<ContainerProps<T>, 'background' | 'border'> {
|
|
4
|
+
elevation?: never;
|
|
5
|
+
variant?: SurfaceVariant;
|
|
6
|
+
}
|
|
7
|
+
interface OverlaySurfaceProps<T extends ContainerElement = 'div'> extends Omit<ContainerProps<T>, 'background' | 'border'> {
|
|
8
|
+
variant: 'overlay';
|
|
9
|
+
elevation?: 'low' | 'medium' | 'high';
|
|
10
|
+
}
|
|
11
|
+
interface FlatSurfacePropsWithRenderFunction {
|
|
12
|
+
children: (props: {
|
|
13
|
+
className: string;
|
|
14
|
+
}) => React.ReactNode;
|
|
15
|
+
elevation?: never;
|
|
16
|
+
variant?: SurfaceVariant;
|
|
17
|
+
}
|
|
18
|
+
interface OverlaySurfacePropsWithRenderFunction {
|
|
19
|
+
children: (props: {
|
|
20
|
+
className: string;
|
|
21
|
+
}) => React.ReactNode;
|
|
22
|
+
variant: 'overlay';
|
|
23
|
+
elevation?: 'low' | 'medium' | 'high';
|
|
24
|
+
}
|
|
25
|
+
type SurfaceProps<T extends ContainerElement = 'div'> = FlatSurfaceProps<T> | OverlaySurfaceProps<T>;
|
|
26
|
+
export declare const Surface: <T extends ContainerElement = "div">(props: SurfaceProps<T> | FlatSurfacePropsWithRenderFunction | OverlaySurfacePropsWithRenderFunction) => React.ReactElement;
|
|
27
|
+
export {};
|