@revenuecat/purchases-ui-js 2.0.2 → 2.0.3

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.
Files changed (127) hide show
  1. package/dist/components/button/ButtonNode.stories.svelte +66 -102
  2. package/dist/components/button/ButtonNode.svelte +2 -27
  3. package/dist/components/footer/Footer.stories.svelte +112 -102
  4. package/dist/components/footer/Footer.svelte +8 -4
  5. package/dist/components/icon/Icon.stories.svelte +100 -0
  6. package/dist/components/icon/Icon.stories.svelte.d.ts +19 -0
  7. package/dist/components/icon/Icon.svelte +73 -0
  8. package/dist/components/icon/Icon.svelte.d.ts +4 -0
  9. package/dist/components/image/ClipPath.svelte +49 -0
  10. package/dist/components/image/ClipPath.svelte.d.ts +9 -0
  11. package/dist/components/image/Image.stories.svelte +83 -188
  12. package/dist/components/image/Image.svelte +152 -136
  13. package/dist/components/image/Image.svelte.d.ts +1 -1
  14. package/dist/components/image/Overlay.svelte +36 -0
  15. package/dist/components/image/Overlay.svelte.d.ts +8 -0
  16. package/dist/components/package/Package.stories.svelte +10 -21
  17. package/dist/components/package/Package.svelte +8 -35
  18. package/dist/components/paywall/Node.svelte +27 -28
  19. package/dist/components/paywall/Node.svelte.d.ts +3 -6
  20. package/dist/components/paywall/Paywall.stories.svelte +36 -140
  21. package/dist/components/paywall/Paywall.svelte +22 -6
  22. package/dist/components/paywall/Paywall.svelte.d.ts +3 -2
  23. package/dist/components/paywall/fixtures/override-paywall.d.ts +2 -0
  24. package/dist/components/paywall/fixtures/override-paywall.js +1310 -0
  25. package/dist/components/paywall/fixtures/stack-paywall.d.ts +2 -0
  26. package/dist/components/paywall/fixtures/stack-paywall.js +5223 -0
  27. package/dist/components/paywall/fixtures/variables.d.ts +261 -0
  28. package/dist/components/paywall/fixtures/variables.js +262 -0
  29. package/dist/components/purchase-button/PurchaseButton.stories.svelte +10 -21
  30. package/dist/components/purchase-button/PurchaseButton.svelte +2 -27
  31. package/dist/components/stack/Stack.stories.svelte +2354 -978
  32. package/dist/components/stack/Stack.svelte +111 -134
  33. package/dist/components/stack/Stack.svelte.d.ts +6 -2
  34. package/dist/components/stack/stack-utils.d.ts +10 -30
  35. package/dist/components/stack/stack-utils.js +77 -255
  36. package/dist/components/text/Text.svelte +3 -37
  37. package/dist/components/text/Text.svelte.d.ts +1 -2
  38. package/dist/components/text/TextNode.stories.svelte +10 -36
  39. package/dist/components/text/TextNode.svelte +25 -28
  40. package/dist/components/text/TextNode.svelte.d.ts +1 -1
  41. package/dist/components/text/text-utils.d.ts +4 -9
  42. package/dist/components/text/text-utils.js +32 -117
  43. package/dist/components/timeline/Timeline.stories.svelte +640 -251
  44. package/dist/components/timeline/Timeline.svelte +42 -28
  45. package/dist/components/timeline/Timeline.svelte.d.ts +1 -1
  46. package/dist/components/timeline/TimelineItem.svelte +80 -112
  47. package/dist/components/timeline/TimelineItem.svelte.d.ts +6 -2
  48. package/dist/components/timeline/timeline-utils.d.ts +24 -6
  49. package/dist/components/timeline/timeline-utils.js +21 -113
  50. package/dist/data/entities.d.ts +19 -135
  51. package/dist/index.d.ts +2 -1
  52. package/dist/index.js +2 -1
  53. package/dist/stores/color-mode.d.ts +1 -1
  54. package/dist/stores/paywall.d.ts +5 -2
  55. package/dist/stores/selected.d.ts +5 -0
  56. package/dist/stores/selected.js +12 -0
  57. package/dist/stores/variables.d.ts +1 -1
  58. package/dist/stores/variables.js +0 -1
  59. package/dist/stories/component-decorator.d.ts +2 -0
  60. package/dist/stories/component-decorator.js +12 -0
  61. package/dist/stories/fixtures.d.ts +5 -3
  62. package/dist/stories/fixtures.js +5214 -4422
  63. package/dist/stories/paywall-decorator.js +6 -0
  64. package/dist/stories/variables-decorator.d.ts +1 -1
  65. package/dist/stories/viewport-decorator.d.ts +2 -0
  66. package/dist/stories/viewport-decorator.js +8 -0
  67. package/dist/stories/viewport-wrapper.svelte +53 -0
  68. package/dist/stories/viewport-wrapper.svelte.d.ts +10 -0
  69. package/dist/stories/with-layout.d.ts +2 -10
  70. package/dist/stories/with-layout.js +3 -5
  71. package/dist/types/alignment.d.ts +5 -3
  72. package/dist/types/background.d.ts +6 -5
  73. package/dist/types/base.d.ts +7 -0
  74. package/dist/types/base.js +1 -0
  75. package/dist/types/colors.d.ts +4 -4
  76. package/dist/types/component.d.ts +6 -2
  77. package/dist/types/components/button.d.ts +4 -1
  78. package/dist/types/components/footer.d.ts +2 -1
  79. package/dist/types/components/icon.d.ts +28 -0
  80. package/dist/types/components/icon.js +1 -0
  81. package/dist/types/components/image.d.ts +20 -0
  82. package/dist/types/components/image.js +1 -0
  83. package/dist/types/components/package.d.ts +2 -1
  84. package/dist/types/components/purchase-button.d.ts +2 -1
  85. package/dist/types/components/stack.d.ts +32 -0
  86. package/dist/types/components/stack.js +1 -0
  87. package/dist/types/components/text.d.ts +20 -0
  88. package/dist/types/components/text.js +1 -0
  89. package/dist/types/components/timeline.d.ts +35 -0
  90. package/dist/types/components/timeline.js +1 -0
  91. package/dist/types/localization.d.ts +2 -1
  92. package/dist/types/media.d.ts +4 -3
  93. package/dist/types/overrides.d.ts +48 -0
  94. package/dist/types/overrides.js +1 -0
  95. package/dist/types/variables.d.ts +13 -0
  96. package/dist/types/variables.js +10 -0
  97. package/dist/types.d.ts +17 -9
  98. package/dist/ui/atoms/typography.stories.svelte +1 -27
  99. package/dist/ui/molecules/button.stories.svelte +3 -8
  100. package/dist/ui/theme/colors.d.ts +0 -6
  101. package/dist/ui/theme/colors.js +1 -1
  102. package/dist/ui/theme/text.d.ts +3 -4
  103. package/dist/ui/theme/utils.d.ts +0 -10
  104. package/dist/ui/theme/utils.js +5 -5
  105. package/dist/utils/background-utils.d.ts +4 -0
  106. package/dist/utils/background-utils.js +39 -0
  107. package/dist/utils/base-utils.d.ts +18 -0
  108. package/dist/utils/base-utils.js +124 -0
  109. package/dist/utils/constants.d.ts +2 -2
  110. package/dist/utils/constants.js +6 -1
  111. package/dist/utils/font-utils.d.ts +4 -0
  112. package/dist/utils/font-utils.js +47 -0
  113. package/dist/utils/style-utils.d.ts +7 -120
  114. package/dist/utils/style-utils.js +22 -302
  115. package/dist/utils/variable-utils.d.ts +1 -22
  116. package/dist/utils/variable-utils.js +28 -24
  117. package/dist/web-components/index.css +1 -1
  118. package/dist/web-components/index.js +1323 -895
  119. package/package.json +34 -24
  120. package/dist/components/button/button-utils.d.ts +0 -2
  121. package/dist/components/button/button-utils.js +0 -19
  122. package/dist/components/image/image-utils.d.ts +0 -19
  123. package/dist/components/image/image-utils.js +0 -33
  124. package/dist/components/purchase-button/purchase-button-utils.d.ts +0 -2
  125. package/dist/components/purchase-button/purchase-button-utils.js +0 -20
  126. package/dist/stories/meta-templates.d.ts +0 -12
  127. package/dist/stories/meta-templates.js +0 -155
@@ -1,157 +1,134 @@
1
1
  <script lang="ts">
2
2
  import Node from "../paywall/Node.svelte";
3
- import {
4
- getBadgeStyles,
5
- getStackBadgeTextStyles,
6
- getStackComponentStyles,
7
- getZStackChildStyles,
8
- } from "./stack-utils";
9
- import Text from "../text/Text.svelte";
10
- import type { StackProps } from "../../data/entities";
11
3
  import { getColorModeContext } from "../../stores/color-mode";
12
- import { getLocalizationContext } from "../../stores/localization";
13
- import { getVariablesContext } from "../../stores/variables";
14
- import { replaceVariables } from "../../utils/variable-utils";
4
+ import { getSelectedStateContext } from "../../stores/selected";
5
+ import type { StackProps } from "../../types/components/stack";
6
+ import { mapBackground } from "../../utils/background-utils";
7
+ import {
8
+ css,
9
+ mapBorder,
10
+ mapBorderRadius,
11
+ mapShadow,
12
+ mapSize,
13
+ mapSpacing,
14
+ px,
15
+ } from "../../utils/base-utils";
16
+ import { getActiveStateProps } from "../../utils/style-utils";
17
+ import { mapBadge, mapDimension, mapLayerAlignment } from "./stack-utils";
15
18
 
16
- const { id, components, badge, zStackChildStyles, ...restProps }: StackProps =
17
- $props();
19
+ interface MiscProps {
20
+ onclick?: () => void;
21
+ }
22
+
23
+ const props: StackProps & MiscProps = $props();
24
+ const selectedState = getSelectedStateContext();
25
+ const {
26
+ components,
27
+ size,
28
+ dimension,
29
+ spacing,
30
+ margin,
31
+ padding,
32
+ background_color,
33
+ background,
34
+ border,
35
+ shape,
36
+ shadow,
37
+ badge,
38
+ onclick,
39
+ } = $derived.by(() => {
40
+ return {
41
+ ...props,
42
+ ...getActiveStateProps($selectedState, props.overrides),
43
+ };
44
+ });
18
45
 
19
46
  const getColorMode = getColorModeContext();
20
47
  const colorMode = $derived(getColorMode());
21
48
 
22
- const stackStyles = $derived(
23
- getStackComponentStyles(colorMode, {
24
- id,
25
- components,
26
- zStackChildStyles,
27
- ...restProps,
28
- }),
29
- );
30
- const badgeStyles = $derived(
31
- getBadgeStyles(colorMode, {
32
- id,
33
- components,
34
- badge,
35
- ...restProps,
49
+ const style = $derived(
50
+ css({
51
+ display: "flex",
52
+ position: "relative",
53
+ width: mapSize(size.width),
54
+ height: mapSize(size.height),
55
+ ...mapDimension(dimension),
56
+ gap: px(spacing),
57
+ margin: mapSpacing(margin),
58
+ padding: mapSpacing(padding),
59
+ ...mapBackground(colorMode, background_color, background),
60
+ border: mapBorder(colorMode, border),
61
+ "border-radius": mapBorderRadius(shape),
62
+ "box-shadow": mapShadow(colorMode, shadow),
36
63
  }),
37
64
  );
38
65
 
39
- const zStackChildrenStyles = $derived(
40
- getZStackChildStyles({
41
- id,
42
- components,
43
- ...restProps,
44
- }),
45
- );
66
+ const getLayerStyle = (index: number): string => {
67
+ if (index === 0 || dimension.type !== "zlayer") {
68
+ return "";
69
+ }
46
70
 
47
- const variables = getVariablesContext();
48
- const { getLocalizedString } = getLocalizationContext();
49
- const label = $derived(
50
- replaceVariables(getLocalizedString(badge?.text_lid), $variables),
51
- );
71
+ const { justifyContent, alignItems } = mapLayerAlignment(
72
+ dimension.alignment,
73
+ );
74
+ return css({
75
+ position: "absolute",
76
+ inset: 0,
77
+ display: "flex",
78
+ "justify-content": justifyContent,
79
+ "align-items": alignItems,
80
+ "z-index": index + 1,
81
+ });
82
+ };
52
83
 
53
- const { tagToRender, textStyles } = $derived(
54
- getStackBadgeTextStyles(colorMode, {
55
- id,
56
- components,
57
- badge,
58
- ...restProps,
59
- }),
60
- );
84
+ const badgeStyle = $derived(css(mapBadge(badge, border?.width)));
61
85
  </script>
62
86
 
63
- <div {id} class="rc-pw-stack-wrapper" style={stackStyles}>
87
+ <svelte:element
88
+ this={onclick !== undefined ? "button" : "div"}
89
+ role={onclick !== undefined ? "button" : undefined}
90
+ {onclick}
91
+ {style}
92
+ class="stack"
93
+ >
64
94
  {#if badge}
65
- <span class="rc-pw-badge" style={badgeStyles}>
66
- <Text styleVariables={textStyles} component={tagToRender}>
67
- {label}
68
- </Text>
69
- </span>
95
+ <div style={badgeStyle}>
96
+ <Node nodeData={badge.stack} />
97
+ </div>
70
98
  {/if}
71
- <div class="rc-pw-stack">
72
- {#each components as component, index}
73
- {#if zStackChildrenStyles}
74
- <Node
75
- nodeData={component}
76
- componentState={restProps.componentState}
77
- zStackChildStyles={index > 0 ? zStackChildrenStyles : undefined}
78
- />
79
- {:else}
80
- <Node nodeData={component} componentState={restProps.componentState} />
81
- {/if}
99
+
100
+ {#if dimension.type === "zlayer"}
101
+ <div style="position: relative; width: 100%; height: 100%;">
102
+ {#each components as component, index}
103
+ <div style={getLayerStyle(index)}>
104
+ <Node nodeData={component} />
105
+ </div>
106
+ {/each}
107
+ </div>
108
+ {:else}
109
+ {#each components as component}
110
+ <Node nodeData={component} />
82
111
  {/each}
83
- </div>
84
- </div>
112
+ {/if}
113
+ </svelte:element>
85
114
 
86
115
  <style>
87
- .rc-pw-stack-wrapper {
88
- display: block;
89
- box-sizing: border-box;
90
- width: var(--stack-width, unset);
91
- height: var(--stack-height, unset);
92
- box-shadow: var(--stack-shadow, none);
93
- background: var(--stack-background, unset);
94
- position: var(--stack-position, relative);
95
- transform: var(--stack-transform, initial);
96
- inset: var(--stack-inset, initial);
97
- margin-block-start: var(--stack-margin-block-start, 0);
98
- margin-inline-end: var(--stack-margin-inline-end, 0);
99
- margin-block-end: var(--stack-margin-block-end, 0);
100
- margin-inline-start: var(--stack-margin-inline-start, 0);
101
- border: var(--stack-border, none);
102
- border-end-start-radius: var(--stack-border-end-start-radius, 0px);
103
- border-end-end-radius: var(--stack-border-end-end-radius, 0px);
104
- border-start-start-radius: var(--stack-border-start-start-radius, 0px);
105
- border-start-end-radius: var(--stack-border-start-end-radius, 0px);
106
- }
116
+ .stack {
117
+ appearance: none;
118
+ position: relative;
107
119
 
108
- .rc-pw-stack {
109
- background: none;
110
- overflow: hidden;
111
- box-sizing: border-box;
112
- width: 100%;
113
- height: 100%;
114
- display: flex;
115
- gap: var(--stack-spacing, 0);
116
- flex: var(--stack-flex, 0 1 auto);
117
- flex-direction: var(--stack-direction, unset);
118
- justify-content: var(--stack-distribution, flex-start);
119
- align-items: var(--stack-alignment, flex-start);
120
- padding-block-start: var(--stack-padding-block-start, 0);
121
- padding-inline-end: var(--stack-padding-inline-end, 0);
122
- padding-block-end: var(--stack-padding-block-end, 0);
123
- padding-inline-start: var(--stack-padding-inline-start, 0);
124
- }
125
- .rc-pw-badge {
126
- box-sizing: border-box;
127
- font-family: var(--stack-badge-font-family, sans-serif);
128
- position: absolute;
129
- inset: var(--stack-badge-inset, unset);
130
- transform: var(--stack-badge-transform, initial);
131
- margin-block-start: var(--stack-badge-margin-block-start, 0px);
132
- margin-inline-end: var(--stack-badge-margin-inline-end, 0px);
133
- margin-block-end: var(--stack-badge-margin-block-end, 0px);
134
- margin-inline-start: var(--stack-badge-margin-inline-start, 0px);
135
- padding-block-start: var(--stack-badge-padding-block-start, 0px);
136
- padding-inline-end: var(--stack-badge-padding-inline-end, 0px);
137
- padding-block-end: var(--stack-badge-padding-block-end, 0px);
138
- padding-inline-start: var(--stack-badge-padding-inline-start, 0px);
139
- background: var(--stack-badge-background, initial);
140
- color: var(--stack-badge-text-color, inherit);
141
- border: var(--stack-badge-border, none);
142
- border-end-start-radius: var(--stack-badge-border-end-start-radius, 0px);
143
- border-end-end-radius: var(--stack-badge-border-end-end-radius, 0px);
144
- border-start-start-radius: var(
145
- --stack-badge-border-start-start-radius,
146
- 0px
147
- );
148
- border-start-end-radius: var(--stack-badge-border-start-end-radius, 0px);
149
- box-shadow: var(--stack-badge-shadow, none);
150
- display: flex;
151
- align-items: center;
152
- justify-content: center;
153
- width: var(--stack-badge-badge-width, max-content);
154
- height: var(--stack-badge-badge-height, max-content);
155
- z-index: var(--stack-badge-z-index, 1);
120
+ &[role="button"] {
121
+ cursor: pointer;
122
+ }
123
+
124
+ &::before {
125
+ content: "";
126
+ position: absolute;
127
+ top: 0;
128
+ left: 0;
129
+ width: 100%;
130
+ height: 100%;
131
+ background: var(--overlay);
132
+ }
156
133
  }
157
134
  </style>
@@ -1,4 +1,8 @@
1
- import type { StackProps } from "../../data/entities";
2
- declare const Stack: import("svelte").Component<StackProps, {}, "">;
1
+ import type { StackProps } from "../../types/components/stack";
2
+ interface MiscProps {
3
+ onclick?: () => void;
4
+ }
5
+ type $$ComponentProps = StackProps & MiscProps;
6
+ declare const Stack: import("svelte").Component<$$ComponentProps, {}, "">;
3
7
  type Stack = ReturnType<typeof Stack>;
4
8
  export default Stack;
@@ -1,31 +1,11 @@
1
- import type { StackProps } from "../../data/entities";
2
- import type { ColorMode } from "../../types";
3
- /**
4
- * Generates comprehensive styles for stack components by combining component, dimension and size styles
5
- * @param props - Stack component properties including background, spacing, size, border etc.
6
- * @returns CSS style variables object with all stack-specific styles
7
- */
8
- export declare const getStackComponentStyles: (colorMode: ColorMode, props: StackProps) => string;
9
- /**
10
- * Generates styles for badge component within a stack
11
- * @param props - Stack component properties containing badge configuration
12
- * @returns CSS style object with badge-specific styles including positioning, dimensions and appearance
13
- */
14
- export declare function getBadgeStyles(colorMode: ColorMode, props: StackProps): string;
15
- /**
16
- * Generates text styles and HTML tag for a stack's badge component
17
- * @param props - Stack component properties containing badge configuration
18
- * @returns Object containing:
19
- * - tagToRender: HTML tag to use for the badge text
20
- * - textStyles: CSS styles string for the badge text
21
- */
22
- export declare function getStackBadgeTextStyles(colorMode: ColorMode, props: StackProps): {
23
- tagToRender: string;
24
- textStyles: string;
25
- };
26
- export declare function getZStackChildStyles(props: StackProps): ZStackChildStyles | undefined;
27
- export type ZStackChildStyles = {
28
- "--inset": string;
29
- "--transform": string;
30
- "--position": string;
1
+ import type { Dimension, ZAlignment } from "../../types/alignment";
2
+ import type { Badge } from "../../types/components/stack";
3
+ import { type CSS } from "../../utils/base-utils";
4
+ export declare function mapDimension(dimension: Dimension): Record<string, string>;
5
+ type JustifyAlign = {
6
+ justifyContent: string;
7
+ alignItems: string;
31
8
  };
9
+ export declare function mapLayerAlignment(alignment: ZAlignment): JustifyAlign;
10
+ export declare function mapBadge(badge?: Badge | null, parentBorderWidth?: number): CSS;
11
+ export {};
@@ -1,261 +1,83 @@
1
- import { getTextComponentStyles } from "../text/text-utils";
2
- import { getActiveStateProps, getComponentStyles, getDimensionStyle, getInsetStyles, getSizeStyle, prefixObject, stringifyStyles, } from "../../utils/style-utils";
3
- /**
4
- * Generates comprehensive styles for stack components by combining component, dimension and size styles
5
- * @param props - Stack component properties including background, spacing, size, border etc.
6
- * @returns CSS style variables object with all stack-specific styles
7
- */
8
- export const getStackComponentStyles = (colorMode, props) => {
9
- const { background_color, margin, padding, spacing, size, border, shape, shadow, dimension, componentState, overrides, zStackChildStyles, } = props;
10
- const stackStyles = {
11
- "--flex": "0 1 auto",
12
- "--height": "initial",
13
- "--width": "initial",
14
- "--spacing": `${spacing || 0}px`,
15
- "--direction": "unset",
16
- "--alignment": "unset",
17
- "--distribution": "flex-start",
18
- "--margin-block-start": "0px",
19
- "--margin-inline-end": "0px",
20
- "--margin-block-end": "0px",
21
- "--margin-inline-start": "0px",
22
- "--padding-block-start": "0px",
23
- "--padding-inline-end": "0px",
24
- "--padding-block-end": "0px",
25
- "--padding-inline-start": "0px",
26
- "--background": "unset",
27
- "--text-color": "inherit",
28
- "--border": "none",
29
- "--border-end-start-radius": "0px",
30
- "--border-end-end-radius": "0px",
31
- "--border-start-start-radius": "0px",
32
- "--border-start-end-radius": "0px",
33
- "--shadow": "none",
34
- "--position": "relative",
35
- "--transform": "initial",
36
- "--inset": "initial",
37
- };
38
- const activeStateProps = getActiveStateProps(overrides, componentState);
39
- Object.assign(stackStyles, getComponentStyles({
40
- background_color,
41
- margin,
42
- padding,
43
- border,
44
- colorMode,
45
- shape,
46
- shadow,
47
- ...activeStateProps,
48
- }), getDimensionStyle({
49
- ...dimension,
50
- ...activeStateProps,
51
- }), getSizeStyle({ ...size, ...activeStateProps }));
52
- Object.assign(stackStyles, zStackChildStyles);
53
- const prefixedStyles = prefixObject(stackStyles, "stack");
54
- return stringifyStyles(prefixedStyles);
55
- };
56
- /**
57
- * Generates styles for badge component within a stack
58
- * @param props - Stack component properties containing badge configuration
59
- * @returns CSS style object with badge-specific styles including positioning, dimensions and appearance
60
- */
61
- export function getBadgeStyles(colorMode, props) {
62
- const { badge } = props;
63
- if (!badge)
64
- return "";
65
- const styles = {
66
- "--inset": "unset",
67
- "--transform": "unset",
68
- "--margin-block-start": "0px",
69
- "--margin-inline-end": "0px",
70
- "--margin-block-end": "0px",
71
- "--margin-inline-start": "0px",
72
- "--padding-block-start": "0px",
73
- "--padding-inline-end": "0px",
74
- "--padding-block-end": "0px",
75
- "--padding-inline-start": "0px",
76
- "--background": "unset",
77
- "--text-color": "inherit",
78
- "--border": "none",
79
- "--border-end-start-radius": "0px",
80
- "--border-end-end-radius": "0px",
81
- "--border-start-start-radius": "0px",
82
- "--border-start-end-radius": "0px",
83
- "--shadow": "none",
84
- "--badge-width": "unset",
85
- "--z-index": "unset",
86
- };
87
- // Calculating this as a zIndex alignment since it behaves in the same way
88
- // except there's no center alignment
89
- Object.assign(styles, getInsetStyles({
90
- type: "zlayer",
91
- alignment: badge.alignment,
92
- }), getComponentStyles({
93
- ...badge,
94
- background_color: badge.background_color,
95
- color: badge.color,
96
- colorMode,
97
- }));
98
- if (badge.style === "overlay") {
99
- // Disable vertical margin for nested & center aligned badges
100
- styles["--margin-block-start"] = "0px";
101
- styles["--margin-block-end"] = "0px";
102
- // Vertically center the badge to the edge of the stack
103
- styles["--transform"] = getBadgeTransformStyles(badge, 0, -(props.border?.width || 0));
1
+ import { px } from "../../utils/base-utils";
2
+ const ALIGNMENT_MAP = Object.freeze({
3
+ leading: "flex-start",
4
+ top: "flex-start",
5
+ center: "center",
6
+ trailing: "flex-end",
7
+ bottom: "flex-end",
8
+ });
9
+ const DISTRIBUTION_MAP = Object.freeze({
10
+ space_between: "space-between",
11
+ space_around: "space-around",
12
+ space_evenly: "space-evenly",
13
+ start: "flex-start",
14
+ center: "center",
15
+ end: "flex-end",
16
+ });
17
+ export function mapDimension(dimension) {
18
+ if (dimension.type === "zlayer") {
19
+ return {};
104
20
  }
105
- if (badge.style === "edge_to_edge") {
106
- styles["--margin-block-start"] = "0px";
107
- styles["--margin-block-end"] = "0px";
108
- styles["--margin-inline-start"] = "0px";
109
- styles["--margin-inline-end"] = "0px";
110
- // Shared props between top and bottom
111
- if (badge.alignment === "top" || badge.alignment === "bottom") {
112
- styles["--badge-width"] =
113
- `calc(100% + ${(props.border?.width || 0) * 2}px)`;
114
- styles["--z-index"] = "-1";
115
- const highestRadius = Math.max(props.shape?.corners?.top_leading || 0, props.shape?.corners?.top_trailing || 0);
116
- // one offs for top and bottom
117
- if (badge.alignment === "top") {
118
- styles["--padding-block-end"] =
119
- `${highestRadius + badge.padding.bottom}px`;
120
- styles["--transform"] = getBadgeTransformStyles(badge, highestRadius);
121
- }
122
- if (badge.alignment === "bottom") {
123
- styles["--padding-block-start"] =
124
- `${highestRadius + badge.padding.top}px`;
125
- styles["--transform"] = getBadgeTransformStyles(badge, -highestRadius);
126
- }
127
- }
128
- else {
129
- styles["--transform"] = getBadgeTransformStyles(badge);
130
- Object.assign(styles, getBadgeBorderRadiusStyles(props));
131
- }
132
- }
133
- const prefixedStyles = prefixObject(styles, "stack-badge");
134
- return stringifyStyles(prefixedStyles);
135
- }
136
- const oppositeCornerDictionary = {
137
- top_leading: "bottom_trailing",
138
- top_trailing: "bottom_leading",
139
- bottom_leading: "top_trailing",
140
- bottom_trailing: "top_leading",
141
- };
142
- const propToCssPropDictionary = {
143
- top_leading: "--border-start-start-radius",
144
- top_trailing: "--border-start-end-radius",
145
- bottom_leading: "--border-end-start-radius",
146
- bottom_trailing: "--border-end-end-radius",
147
- };
148
- function getBadgeBorderRadiusStyles(props) {
149
- const styles = {
150
- "--border-end-start-radius": "0px",
151
- "--border-end-end-radius": "0px",
152
- "--border-start-start-radius": "0px",
153
- "--border-start-end-radius": "0px",
21
+ const direction = dimension.type === "vertical" ? "column" : "row";
22
+ const justify = DISTRIBUTION_MAP[dimension.distribution] || "center";
23
+ const align = ALIGNMENT_MAP[dimension.alignment] || "center";
24
+ return {
25
+ "flex-direction": direction,
26
+ "justify-content": justify,
27
+ "align-items": align,
154
28
  };
155
- const { badge } = props;
156
- if (!badge)
157
- return styles;
158
- const borderWidth = props.border?.width || 0;
159
- const badgeAlignment = badge.alignment;
160
- // badge inner radius
161
- styles[propToCssPropDictionary[oppositeCornerDictionary[badgeAlignment]]] =
162
- `${badge.shape?.corners[oppositeCornerDictionary[badgeAlignment]]}px`;
163
- // badge outer radius
164
- styles[propToCssPropDictionary[badgeAlignment]] =
165
- `${props.shape?.corners[badgeAlignment] - borderWidth}px`;
166
- return styles;
167
29
  }
168
- /**
169
- * Generates CSS transform styles for badge positioning
170
- * @param badge - Badge configuration from StackProps
171
- * @param verticalOffset - Optional vertical offset in pixels for edge-to-edge badges
172
- * @returns CSS transform string for badge positioning
173
- */
174
- function getBadgeTransformStyles(badge, verticalOffset = 0, horizontalOffset = 0) {
175
- if (!badge)
176
- return "";
177
- if (badge.style === "overlay") {
178
- if (badge.alignment === "top_leading") {
179
- return `translate(${horizontalOffset}px, calc(-50% + ${verticalOffset}px))`;
180
- }
181
- if (badge.alignment === "top_trailing") {
182
- return `translate(calc(0% - ${horizontalOffset}px), calc(-50% + ${verticalOffset}px))`;
183
- }
184
- if (badge.alignment === "bottom_leading") {
185
- return `translate(${horizontalOffset}px, calc(50% + ${verticalOffset}px))`;
186
- }
187
- if (badge.alignment === "bottom_trailing") {
188
- return `translate(calc(0% - ${horizontalOffset}px), calc(50% + ${verticalOffset}px))`;
189
- }
190
- if (badge.alignment === "top") {
191
- return `translate(calc(-50% + ${horizontalOffset}px), calc(-50% + ${verticalOffset}px))`;
192
- }
193
- if (badge.alignment === "bottom") {
194
- return `translate(calc(50% + ${horizontalOffset}px), calc(50% + ${verticalOffset}px))`;
195
- }
30
+ const Z_ALIGNMENT_MAP = Object.freeze({
31
+ top_leading: { justifyContent: "flex-start", alignItems: "flex-start" },
32
+ top: { justifyContent: "center", alignItems: "flex-start" },
33
+ top_trailing: { justifyContent: "flex-end", alignItems: "flex-start" },
34
+ leading: { justifyContent: "flex-start", alignItems: "center" },
35
+ center: { justifyContent: "center", alignItems: "center" },
36
+ trailing: { justifyContent: "flex-end", alignItems: "center" },
37
+ bottom_leading: { justifyContent: "flex-start", alignItems: "flex-end" },
38
+ bottom: { justifyContent: "center", alignItems: "flex-end" },
39
+ bottom_trailing: { justifyContent: "flex-end", alignItems: "flex-end" },
40
+ });
41
+ export function mapLayerAlignment(alignment) {
42
+ return Z_ALIGNMENT_MAP[alignment];
43
+ }
44
+ export function mapBadge(badge, parentBorderWidth) {
45
+ if (badge == null) {
46
+ return {};
196
47
  }
197
- if (badge.style === "edge_to_edge") {
198
- if (badge.alignment === "top") {
199
- return `translate(calc(-50% + ${horizontalOffset}px), calc(-100% - ${verticalOffset}px + 16px))`;
200
- }
201
- else if (badge.alignment === "bottom") {
202
- return `translate(calc(50% + ${horizontalOffset}px), calc(100% + ${verticalOffset}px))`;
203
- }
48
+ const { style, alignment } = badge;
49
+ const isTop = alignment.startsWith("top");
50
+ const isLeading = alignment.endsWith("leading");
51
+ const isTrailing = alignment.endsWith("trailing");
52
+ const isCenter = !isLeading && !isTrailing;
53
+ const offset = parentBorderWidth ?? 0;
54
+ if (style === "overlay") {
55
+ const translateX = isCenter ? "translateX(-50%)" : "";
56
+ const translateY = isTop ? "translateY(-50%)" : "translateY(50%)";
57
+ return {
58
+ "z-index": 10,
59
+ position: "absolute",
60
+ width: "max-content",
61
+ top: isTop ? px(-offset / 2) : "initial",
62
+ bottom: !isTop ? px(-offset / 2) : "initial",
63
+ left: isLeading ? px(-offset) : isCenter ? "50%" : "initial",
64
+ right: isTrailing ? px(-offset) : "initial",
65
+ transform: `${translateX} ${translateY}`,
66
+ };
204
67
  }
205
- return "";
206
- }
207
- /**
208
- * Generates text styles and HTML tag for a stack's badge component
209
- * @param props - Stack component properties containing badge configuration
210
- * @returns Object containing:
211
- * - tagToRender: HTML tag to use for the badge text
212
- * - textStyles: CSS styles string for the badge text
213
- */
214
- export function getStackBadgeTextStyles(colorMode, props) {
215
- const { badge } = props;
216
- if (!badge)
217
- return { tagToRender: "", textStyles: "" };
218
- const { tagToRender, textStyles } = getTextComponentStyles(colorMode, {
219
- id: props.id,
220
- ...badge,
221
- type: "text",
222
- size: {
223
- width: { type: "fit" },
224
- height: { type: "fit" },
225
- },
226
- color: badge?.color,
227
- name: props.name,
228
- });
229
- const resetSpacing = {
230
- "--text-margin-block-start": "0px",
231
- "--text-margin-inline-end": "0px",
232
- "--text-margin-block-end": "0px",
233
- "--text-margin-inline-start": "0px",
234
- "--text-padding-block-start": "0px",
235
- "--text-padding-inline-end": "0px",
236
- "--text-padding-block-end": "0px",
237
- "--text-padding-inline-start": "0px",
238
- };
239
- const stylesObject = {
240
- ...textStyles,
241
- ...resetSpacing,
242
- };
243
- const stringifiedStyles = stringifyStyles(stylesObject);
244
- return {
245
- tagToRender,
246
- textStyles: stringifiedStyles,
247
- };
248
- }
249
- export function getZStackChildStyles(props) {
250
- const { dimension } = props;
251
- const baseStyles = {
252
- "--inset": "initial",
253
- "--transform": "initial",
254
- "--position": "relative",
255
- };
256
- if (dimension.type !== "zlayer")
257
- return;
258
- const insetStyles = getInsetStyles({ ...dimension, type: "zlayer" });
259
- Object.assign(baseStyles, { ...insetStyles, "--position": "absolute" });
260
- return baseStyles;
68
+ if (style === "nested") {
69
+ return {
70
+ "z-index": 10,
71
+ position: "absolute",
72
+ inset: 0,
73
+ display: "flex",
74
+ top: isTop ? px(offset) : "initial",
75
+ bottom: !isTop ? px(offset) : "initial",
76
+ left: isLeading ? px(offset) : isCenter ? "50%" : "initial",
77
+ right: isTrailing ? px(offset) : "initial",
78
+ transform: isCenter ? "translateX(-50%)" : "initial",
79
+ };
80
+ }
81
+ // Not supported yet.
82
+ return {};
261
83
  }