Skip to content

Accept Marker type in StyleXStyles for passing between component prop boundaries - #1820

Open
titchimoto wants to merge 3 commits into
facebook:mainfrom
titchimoto:rd-define-marker-type-updates
Open

Accept Marker type in StyleXStyles for passing between component prop boundaries#1820
titchimoto wants to merge 3 commits into
facebook:mainfrom
titchimoto:rd-define-marker-type-updates

Conversation

@titchimoto

@titchimoto titchimoto commented Aug 25, 2026

Copy link
Copy Markdown

What changed / motivation ?

In our Design System, we follow StyleX's docs recommend pattern for constraining component style props using StyleXStyles<T>:

// Design system component
type BoxProps = {
  /** Only layout properties allowed, other styles are intentionally blocked */
  xstyle?: StyleXStyles<{
    margin?: CSSProperties['margin'];
    padding?: CSSProperties['padding'];
    flexGrow?: CSSProperties['flexGrow'];
  }>;
};

This pattern works for style overrides but consumers also need to use the marker API to implement ancestor-state patterns, for example, revealing a child element when a parent is hovered, entirely in CSS:

const styles = stylex.create({
  actions: {
    opacity: {
      default: 0,
      [stylex.when.ancestor(':hover')]: 1,
    },
  },
});

// Mark the parent component as the hover anchor...
<Box xstyle={stylex.defaultMarker()}>  {/* ❌ Type error */}
  <ChildComponent xstyle={styles.actions} />
</Box>

// ...or compose layout styles and a marker together
<Box xstyle={[styles.layout, stylex.defaultMarker()]}>  {/* ❌ Type error */}
  <ChildComponent xstyle={styles.actions} />
</Box>

Because StyleXStyles<T> has no way to express "a marker token is also valid here", in our DS, we've created an internal XStyle type which is essentially a union of StylexStyles<T> | StyleXMarkerToken (which we've just created internally from the ReturnType's as in this PR), which does work & without that our DS consumers are forced to either add additional wrapping elements, or are generally unable to use our components for their use-case.

But it would be nice to not have to manage this at the DS level & could live in the library instead to pass between component prop boundaries easier.

Pre-flight checklist

@meta-cla

meta-cla Bot commented Aug 25, 2026

Copy link
Copy Markdown

Hi @titchimoto!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@vercel

vercel Bot commented Aug 25, 2026

Copy link
Copy Markdown

@titchimoto is attempting to deploy a commit to the Meta Open Source Team on Vercel.

A member of the Team first needs to authorize it.

@nmn
nmn marked this pull request as ready for review August 31, 2026 07:24
@nmn
nmn requested review from mellyeliu and nmn as code owners August 31, 2026 07:24
Copilot AI lite review requested due to automatic review settings August 31, 2026 07:24

@nmn nmn left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Default markers and custom markers should really be combined into a single type.

Comment on lines +318 to +329
@@ -318,6 +319,14 @@ export type StyleX$DefineMarker = () => MapNamespace<{
readonly marker: symbol;
}>;

export type StyleX$DefaultMarker = () => MapNamespace<{
readonly marker: 'default-marker';
}>;

export type StyleXMarkerToken =
| ReturnType<StyleX$DefineMarker>
| ReturnType<StyleX$DefaultMarker>;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
export type StyleXMarker = MapNamespace<{
readonly marker: symbol;
}>;
export type StyleX$DefineMarker = () => StyleXMarker;
export type StyleX$DefaultMarker = () => StyleXMarker;

| GenStylePropType<Readonly<CSS>>
| Readonly<[GenStylePropType<Readonly<CSS>>, InlineStyles]>,
| Readonly<[GenStylePropType<Readonly<CSS>>, InlineStyles]>
| StyleXMarkerToken,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
| StyleXMarkerToken,
| StyleXMarker,

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR updates StyleX’s public typing so marker tokens (stylex.defaultMarker() / stylex.defineMarker()) are accepted anywhere StyleXStyles<T> is used, enabling marker-based ancestor/descendant styling patterns to pass cleanly through constrained component style props (e.g., design-system xstyle?: StyleXStyles<...>).

Changes:

  • Extend StyleXStyles<T> to accept a new StyleXMarkerToken union type.
  • Define and export StyleXMarkerToken (and StyleX$DefaultMarker) in both Flow and TypeScript type surfaces.
  • Add TypeScript type-tests validating marker tokens work with constrained StyleXStyles<T> without weakening CSS constraints.

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated no comments.

File Description
packages/typescript-tests/src/typetests.ts Adds TS type-tests proving marker tokens satisfy StyleXMarkerToken and are accepted by constrained StyleXStyles<T> while preserving constraints.
packages/@stylexjs/stylex/src/types/StyleXTypes.js Updates Flow StyleXStyles to include StyleXMarkerToken and introduces StyleX$DefaultMarker + StyleXMarkerToken type exports.
packages/@stylexjs/stylex/src/types/StyleXTypes.d.ts Updates TS StyleXStyles to include StyleXMarkerToken and introduces StyleX$DefaultMarker + StyleXMarkerToken type exports.
packages/@stylexjs/stylex/src/stylex.js Re-exports StyleXMarkerToken and types defaultMarker on the legacy interface via StyleX$DefaultMarker.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@titchimoto
titchimoto force-pushed the rd-define-marker-type-updates branch from f3b0864 to 3673e82 Compare September 2, 2026 15:48
@titchimoto

Copy link
Copy Markdown
Author

Default markers and custom markers should really be combined into a single type.

@nmn sounds good! updated here to collapse both these marker types down into a single type. I did keep the Flow custom-marker type in both cases now if that works? but I think that should keep these both closer in sync now.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 2, 2026
@meta-cla

meta-cla Bot commented Sep 2, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants