-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
fix(extendVariants): return component type error #5795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@heroui/system-rsc": patch | ||
| --- | ||
|
|
||
| fix extendVariants return type error(#5778) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
Verify that required component props remain required.
The new mapped type makes all keys optional with
?:, including props fromCP(component props). If the base component has required props, marking them optional could weaken type safety and allow consumers to omit required props without type errors, leading to runtime failures.Run the following script to check for components with required props that use
extendVariants:Additionally, consider testing this change with a component that has required props to confirm they are correctly enforced:
If this weakens type safety unintentionally, consider preserving the required/optional nature of
CPprops while addingVprops as optional:🏁 Script executed:
Length of output: 27
🏁 Script executed:
Length of output: 3378
Preserve required component props in extendVariants mapping
The current
[key in keyof CP | keyof V]?makes all CP props optional. Update to:so CP’s required props stay required and only V’s props are optional.
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works fine for me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ITBoomBKStudio, thank you for confirming! If you've tested this and it's working as expected for your use cases, I'll defer to your judgment on this change.
(ᵔᴥᵔ)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also fix error which appears when I try to specify classes for slots in compoundVariants inside extendVariants:
compoundVariants: [
{
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Length of output: 0
🏁 Script executed:
Length of output: 2461
🏁 Script executed:
Length of output: 0
@ITBoomBKStudio, I can see the issue! The
CompoundVariantstype currently only acceptsClassValue(simple strings/arrays), but when you have slots, it should also acceptSlotsClassValue<S>(objects with slot keys liketrigger,value).Here's the fix needed in
packages/core/system-rsc/src/extend-variants.d.ts:1. Update the
CompoundVariantstype to accept slots (Line 51):2. Update the
ExtendVariantssignature to passStoCompoundVariants(Line 99):This change makes
CompoundVariantsslot-aware, so when slots are defined, theclassproperty in compound variants can accept either:ClassValue)SlotsClassValue<S>) like your example withtriggerandvalueThis follows the same pattern used by the
VariantsandGetSuggestedValuestypes in the file.🧬 Code Graph Analysis Results
File: packages/core/system-rsc/src/extend-variants.d.ts, snippet: CompoundVariants type
Description: Defines how compoundVariants are structured as an array of objects combining VariantValue with ClassProp.
Snippet (complete as shown):
type CompoundVariants<V, SV> = Array<VariantValue<V, SV> & ClassProp>;
Location: packages/core/system-rsc/src/extend-variants.d.ts, around the definition of CompoundVariants.
File: packages/core/system-rsc/src/extend-variants.d.ts, snippet: ExtendVariantProps
Description: Typing for the options exposed by ExtendVariants, including compoundVariants having a particular shape.
Snippet (complete as shown):
export type ExtendVariantProps = {
variants?: Record<string, Record<string, string>>;
defaultVariants?: Record<string, string>;
compoundVariants?: Array<Record<string, boolean | string | Record<string, string>>>;
};
Location: packages/core/system-rsc/src/extend-variants.d.ts, near the section where ExtendVariantProps is defined.
File: packages/core/system-rsc/src/extend-variants.d.ts, snippet: ExtendVariants signature (generic parameters)
Description: Core function type signature for extendVariants, showing expected props shape including variants, defaultVariants, compoundVariants, and slots.
Snippet (complete as shown):
export type ExtendVariants = {
<
C extends JSXElementConstructor,
CP extends ComponentProps,
S extends ComponentSlots,
V extends ComposeVariants<CP, S>,
SV extends SuggestedVariants<CP, S>,
DV extends DefaultVariants<V, SV>,
CV extends CompoundVariants<V, SV>,
Location: packages/core/system-rsc/src/extend-variants.d.ts, near the end where ExtendVariants is defined.
File: packages/core/system-rsc/src/extend-variants.js, snippet: runtime handling of variants (compoundVariants)
Description: Shows how the runtime extendVariants function destructures its styles and prepares to compute class names, including handling of compoundVariants.
Snippet (lines 104-150, as provided):
export function extendVariants(BaseComponent, styles = {}, opts = {}) {
const {variants, defaultVariants, compoundVariants} = styles || {};
const slots = getSlots(variants);
const hasSlots = typeof slots === "object" && Object.keys(slots).length !== 0;
const ForwardedComponent = React.forwardRef((originalProps = {}, ref) => {
const newProps = React.useMemo(() =>
getClassNamesWithProps(
{
slots,
variants,
compoundVariants,
props: originalProps,
defaultVariants,
hasSlots,
opts,
},
[originalProps],
),
);
});
// Add collection node function for collection-based components
if (BaseComponent.getCollectionNode) {
ForwardedComponent.getCollectionNode = (itemProps) => {
const newProps = getClassNamesWithProps({
slots,
variants,
compoundVariants,
props: itemProps,
defaultVariants,
hasSlots,
opts,
});
}
// To make dev tools show a proper name
ForwardedComponent.displayName =
Extended(${BaseComponent.displayName || BaseComponent.name});return ForwardedComponent;
}
Location: packages/core/system-rsc/src/extend-variants.js, lines 104-150.