Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(core): type interactionOverlayStyles as StyleX styles so they fit xstyle (#9440)

Cindy Zhang9297ef4a464d
diff --git a/.changeset/interaction-overlay-styles-xstyle-type.md b/.changeset/interaction-overlay-styles-xstyle-type.md
new file mode 100644
index 0000000000..b1b3e21f29
--- /dev/null
+++ b/.changeset/interaction-overlay-styles-xstyle-type.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] TypeScript users applying `interactionOverlayStyles` through an Astryx component's `xstyle` no longer get a type error. The touch-press properties of each variant (`animationName`, `animationDuration`, `animationTimingFunction`, `animationFillMode`, and the colour variants' `backgroundImage`) now carry their CSS value types instead of `unknown`. The generated CSS is unchanged.
+@cixzhang
diff --git a/packages/core/api-report.md b/packages/core/api-report.md
index 16e7e80f24..abe9766bdd 100644
--- a/packages/core/api-report.md
+++ b/packages/core/api-report.md
@@ -3346,7 +3346,7 @@ const inputStatusFocusStyles: Readonly<{ readonly warning: Readonly<{ readonly b
const inputStatusFocusWithinStyles: Readonly<{ readonly warning: Readonly<{ readonly borderColor: stylex.StyleXClassNameFor<"borderColor", "var(--color-text-warning)"> }>; readonly error: Readonly … #a9ac1cbc
const inputStatusHoverShadowStyles: Readonly<{ readonly warning: Readonly<{ readonly boxShadow: stylex.StyleXClassNameFor<"boxShadow", "none" | { readonly '@media (hover: hover)': stylex.StyleXVar … #de6f47e5
const inputWrapperStyles: Readonly<{ readonly base: Readonly<{ readonly boxSizing: stylex.StyleXClassNameFor<"boxSizing", "border-box">; readonly position: stylex.StyleXClassNameFor<"pos … #2e7e1cc8
-const interactionOverlayStyles: Readonly<{ readonly backgroundColor: Readonly<{ readonly backgroundColor: stylex.StyleXClassNameFor<"backgroundColor", "light-dark(#0536590C, #FFFFFF0C)" | "lig … #4cc12b9b
+const interactionOverlayStyles: Readonly<{ readonly backgroundColor: Readonly<{ readonly backgroundColor: stylex.StyleXClassNameFor<"backgroundColor", "light-dark(#0536590C, #FFFFFF0C)" | "lig … #3522ad34
const overlayPaddingReset: Readonly<{ readonly reset: Readonly<{ readonly '--container-padding-inline-start': stylex.StyleXClassNameFor<"--container-padding-inline-start", "0px">; readonl … #15cd7287
const radiusDefaults: object
--radius-none: '0px'
diff --git a/packages/core/src/utils/interactionOverlay.stylex.ts b/packages/core/src/utils/interactionOverlay.stylex.ts
index 02aa8b243a..d2fde43b41 100644
--- a/packages/core/src/utils/interactionOverlay.stylex.ts
+++ b/packages/core/src/utils/interactionOverlay.stylex.ts
@@ -99,10 +99,14 @@ const COARSE = '@media (pointer: coarse)';
* attribute is the system's own (`data-astryx-*`, like the pressable marker),
* outside the namespace a theme's state keys generate into (`[data-<state>]`),
* so a theme state can never collide with it.
+ *
+ * The press selectors are typed `string`, not as literals. As a computed key,
+ * a `string` gives the object an index signature, and StyleX reads the value
+ * type from it. See {@link ByAttribute}.
*/
-const PRESSED_ON = '[data-astryx-press="on"]';
+const PRESSED_ON: string = '[data-astryx-press="on"]';
/** Written by the touch press controller for the release's exit. */
-const PRESSED_FADING = '[data-astryx-press="fading"]';
+const PRESSED_FADING: string = '[data-astryx-press="fading"]';
const PRESS_ALPHA = pressVars['--astryx-press-alpha'];
@@ -163,7 +167,28 @@ const pressStrength = {
'[data-astryx-press="on"]': 1,
'[data-astryx-press="fading"]': 1,
};
-const pressReleaseAnimation = {
+/**
+ * A value that only attribute selectors choose, such as the press arms.
+ *
+ * StyleX infers a value's type from its `default`, its `:` and `@`
+ * conditions, and an index signature. Literal attribute keys are none of
+ * these. For a property whose conditions are all literal attribute keys, it
+ * infers `unknown`, and no component's `xstyle` accepts that. This type gives
+ * the hoisted press arms an index signature.
+ */
+type ByAttribute<Value> = Readonly<{
+ default: null;
+ [attribute: string]: Value | null;
+}>;
+
+const pressReleaseAnimation: Readonly<{
+ animationName: ByAttribute<string>;
+ animationDuration: ByAttribute<
+ Readonly<{default: string; [media: `@media ${string}`]: string}>
+ >;
+ animationTimingFunction: ByAttribute<string>;
+ animationFillMode: ByAttribute<'both'>;
+}> = {
animationName: {
default: null,
'[data-astryx-press="fading"]': pressRelease,
diff --git a/packages/core/src/utils/interactionOverlayXstyle.test.tsx b/packages/core/src/utils/interactionOverlayXstyle.test.tsx
new file mode 100644
index 0000000000..3e6e4375b8
--- /dev/null
+++ b/packages/core/src/utils/interactionOverlayXstyle.test.tsx
@@ -0,0 +1,46 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file interactionOverlayXstyle.test.tsx
+ * @input interactionOverlayStyles and an Astryx component's `xstyle` prop
+ * @output Every overlay variant is a StyleX style that a component's `xstyle`
+ * accepts. The type check is the proof: `pnpm typecheck` compiles this file.
+ * @position Type test of packages/core/src/utils/interactionOverlay.stylex.ts
+ */
+
+import {render, screen} from '@testing-library/react';
+import {describe, expect, it} from 'vitest';
+import {Card} from '../Card/Card';
+import {interactionOverlayStyles} from './interactionOverlay.stylex';
+
+describe('interactionOverlayStyles', () => {
+ it('fits the xstyle prop of an Astryx component', () => {
+ render(
+ <>
+ <Card
+ data-testid="variant"
+ xstyle={interactionOverlayStyles.backgroundColor}
+ />
+ <Card
+ data-testid="variant"
+ xstyle={interactionOverlayStyles.backgroundImage}
+ />
+ <Card
+ data-testid="variant"
+ xstyle={interactionOverlayStyles.backgroundImageOnNeutral}
+ />
+ <Card
+ data-testid="variant"
+ xstyle={interactionOverlayStyles.pressedBackgroundColor}
+ />
+ <Card
+ data-testid="variant"
+ xstyle={interactionOverlayStyles.pressedAlpha}
+ />
+ </>,
+ );
+ expect(screen.getAllByTestId('variant')).toHaveLength(
+ Object.keys(interactionOverlayStyles).length,
+ );
+ });
+});