Skip to content

poc for content position controlled by selected items#1144

Merged
kodiakhq[bot] merged 4 commits intomainfrom
feat/poc-select-content-postion
Jun 8, 2025
Merged

poc for content position controlled by selected items#1144
kodiakhq[bot] merged 4 commits intomainfrom
feat/poc-select-content-postion

Conversation

@GoldGroove06
Copy link
Copy Markdown
Collaborator

@GoldGroove06 GoldGroove06 commented Jun 6, 2025

#1127

currently not working with portal but its a poc, improvements and recommendations are welcomed

Recording.2025-06-07.023408.mp4

Summary by CodeRabbit

  • New Features

    • Added a new story, "BasicPortal", to showcase the Select dropdown without a portal wrapper.
    • Enhanced Select dropdown positioning by introducing an optional offset property for more flexible placement.
  • Bug Fixes

    • Removed an unnecessary console log from the Select trigger component.
  • Refactor

    • Improved internal structure and state management for Select dropdown positioning and context sharing.
    • Cleaned up whitespace and formatting in several files for consistency.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 6, 2025

Walkthrough

This update introduces enhancements to the Select component system, notably adding dynamic offset positioning for dropdowns and a new story for portal-less rendering. Several context and prop types are extended to support these features. Minor code cleanups, such as removing extraneous whitespace and logging, are also included.

Changes

File(s) Change Summary
src/core/primitives/Select/fragments/SelectPrimitiveRoot.tsx, .../contexts/SelectPrimitiveContext.tsx Added offsetValue prop, dynamic offset logic, and new context properties for selected item ref and offset setter.
src/core/primitives/Select/fragments/SelectPrimitiveItem.tsx Utilizes new selectedItemRef from context for assigning ref to selected item.
src/components/ui/Select/stories/Select.stories.tsx Added new BasicPortal story for Select without a Portal wrapper.
src/components/ui/Select/fragments/SelectContent.tsx Removed an unnecessary blank line in JSX.
src/components/ui/Select/fragments/SelectRoot.tsx Added useEffect to React imports.
src/components/ui/Select/fragments/SelectTrigger.tsx Removed a console.log statement for triggerRef.
src/core/hooks/useIsInsideFrom/index.ts Reformatted code with whitespace and indentation adjustments only.

Sequence Diagram(s)

sequenceDiagram
    participant App
    participant SelectRoot
    participant SelectPrimitiveRoot
    participant SelectPrimitiveItem

    App->>SelectRoot: Render with offsetValue
    SelectRoot->>SelectPrimitiveRoot: Pass offsetValue prop
    SelectPrimitiveRoot->>SelectPrimitiveItem: Provide context with selectedItemRef, setOffsetPositionValue
    SelectPrimitiveItem->>SelectPrimitiveRoot: Assign ref if item is selected
    Note over SelectPrimitiveRoot: useEffect updates offset based on selectedIndex and dropdown height
Loading

Possibly related PRs

  • Select primitive t1 #1117: Introduces the full Select primitive component system and its subcomponents, which this PR builds upon and modifies.

Suggested reviewers

  • kotAPI

Poem

In the garden of code where dropdowns bloom,
A rabbit hops with offset in the room.
Stories grow with portals new and bright,
Contexts stretch, and refs take flight.
Console logs vanish, whitespace aligns—
Hooray for clean and clever designs! 🐇✨

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

npm error Exit handler never called!
npm error This is an error with npm itself. Please report this error at:
npm error https://github.com/npm/cli/issues
npm error A complete log of this run can be found in: /.npm/_logs/2025-06-08T08_30_56_001Z-debug-0.log


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f057e3f and e90016d.

📒 Files selected for processing (1)
  • src/core/primitives/Select/fragments/SelectPrimitiveRoot.tsx (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/core/primitives/Select/fragments/SelectPrimitiveRoot.tsx
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 5

🧹 Nitpick comments (7)
src/components/ui/Select/stories/Select.stories.tsx (1)

34-34: Consider renaming the story for clarity.

The name BasicPortal is misleading since this story specifically demonstrates usage without a portal. Consider renaming to BasicWithoutPortal or BasicNoPortal to better reflect its purpose.

-export const BasicPortal = () => {
+export const BasicWithoutPortal = () => {
src/components/ui/Select/fragments/SelectContent.tsx (2)

16-16: Remove debugging console.log.

The console.log statement should be removed before merging to production.

-                console.log(i);

10-20: Consider more robust selected item detection.

The current logic relies on aria-selected="true" being present on mount, which may not always be reliable. Consider using the component's value/defaultValue props instead of DOM inspection.

-    useEffect(() => {
-        if (!contentRef.current) return;
-       
-        for (let i = 0; i < contentRef.current?.children.length; i++) {
-            if (contentRef.current?.children[i].getAttribute('aria-selected') === 'true') {
-                console.log(i);
-                setSelectedId(i+1);
-            }
-        }
-    },[])
+    useEffect(() => {
+        // Consider using value/defaultValue from Select context instead
+        // of DOM inspection for more reliable selected item detection
+        if (!contentRef.current) return;
+       
+        for (let i = 0; i < contentRef.current?.children.length; i++) {
+            if (contentRef.current?.children[i].getAttribute('aria-selected') === 'true') {
+                setSelectedId(i + 1); // Consider if this should be zero-based
+                break; // Exit loop once selected item is found
+            }
+        }
+    }, [])
src/components/ui/Select/fragments/SelectRoot.tsx (3)

1-1: Remove unused imports.

The useCallback and useEffect imports are not used in this component.

-import React, { useState, useCallback, useEffect } from 'react';
+import React, { useState } from 'react';

11-11: Remove debugging console.log.

The console.log statement should be removed before merging to production.

-    console.log(selectedId)

20-20: Consider making the offset multiplier configurable.

The hardcoded 40 pixel offset should be configurable to support different item heights or design requirements.

+const ITEM_HEIGHT = 40; // or make this a prop
+
-                offsetValue={-(40*selectedId)}
+                offsetValue={selectedId ? -(ITEM_HEIGHT * selectedId) : 0}
src/core/primitives/Select/fragments/SelectPrimitiveRoot.tsx (1)

23-23: Fix formatting issue in parameter destructuring.

There's a missing space before offsetValue in the parameter list.

-function SelectPrimitiveRoot({ children, className, value, name, defaultValue = '', onValueChange, onClickOutside = () => {}, placement = 'bottom-start',offsetValue, ...props }: SelectPrimitiveRootProps) {
+function SelectPrimitiveRoot({ children, className, value, name, defaultValue = '', onValueChange, onClickOutside = () => {}, placement = 'bottom-start', offsetValue, ...props }: SelectPrimitiveRootProps) {
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3479ef3 and c45bdd6.

📒 Files selected for processing (5)
  • src/components/ui/Select/contexts/SelectRootContext.tsx (1 hunks)
  • src/components/ui/Select/fragments/SelectContent.tsx (2 hunks)
  • src/components/ui/Select/fragments/SelectRoot.tsx (2 hunks)
  • src/components/ui/Select/stories/Select.stories.tsx (1 hunks)
  • src/core/primitives/Select/fragments/SelectPrimitiveRoot.tsx (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
src/components/ui/Select/fragments/SelectRoot.tsx (1)
src/components/ui/Select/contexts/SelectRootContext.tsx (1)
  • SelectRootContext (9-13)
src/components/ui/Select/fragments/SelectContent.tsx (1)
src/components/ui/Select/contexts/SelectRootContext.tsx (1)
  • SelectRootContext (9-13)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build
🔇 Additional comments (3)
src/components/ui/Select/fragments/SelectContent.tsx (1)

17-17: Clarify the index calculation logic.

The i+1 calculation seems arbitrary. Consider adding a comment explaining why the index is incremented, or if this should be just i for zero-based indexing.

Can you verify if this should be 1-based indexing? The offset calculation in SelectRoot.tsx uses -(40*selectedId), so understanding the intended indexing is crucial for correct positioning.

src/components/ui/Select/fragments/SelectRoot.tsx (1)

20-20: ⚠️ Potential issue

Fix null handling in offset calculation.

When selectedId is null (initial state), the calculation -(40*selectedId) results in NaN, which could cause positioning issues.

-                offsetValue={-(40*selectedId)}
+                offsetValue={selectedId ? -(40 * selectedId) : 0}

Likely an incorrect or invalid review comment.

src/core/primitives/Select/fragments/SelectPrimitiveRoot.tsx (1)

17-17: Good addition of the offsetValue prop.

The new optional offsetValue prop is appropriately typed and aligns with the POC objective of controlling content position.


function SelectContent({ customRootClass, children, position = 'popper', ...props }: any) {
const { rootClass } = useContext(SelectRootContext);
const { rootClass, setSelectedId } = useContext(SelectRootContext);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix type mismatch with context.

The code extracts setSelectedId from SelectRootContext, but this property is commented out in the context type definition (SelectRootContext.tsx lines 5-6). This will cause TypeScript errors.

-    const { rootClass, setSelectedId } = useContext(SelectRootContext);
+    const { rootClass, setSelectedId } = useContext(SelectRootContext) as any; // TODO: Fix context types

Alternatively, update the context type definition to include these properties.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const { rootClass, setSelectedId } = useContext(SelectRootContext);
const { rootClass, setSelectedId } = useContext(SelectRootContext) as any; // TODO: Fix context types
🤖 Prompt for AI Agents
In src/components/ui/Select/fragments/SelectContent.tsx at line 7, the code
destructures setSelectedId from SelectRootContext, but setSelectedId is
commented out in the SelectRootContext type definition (around lines 5-6 in
SelectRootContext.tsx), causing a type mismatch. To fix this, update the
SelectRootContext type definition to include setSelectedId and rootClass
properties with their correct types, ensuring the context provides these values
and TypeScript recognizes them.


return (
<SelectRootContext.Provider value={{ rootClass }}>
<SelectRootContext.Provider value={{ rootClass, selectedId, setSelectedId }}>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix context type mismatch.

The context value includes selectedId and setSelectedId, but these are commented out in the SelectRootContextType interface, causing TypeScript errors.

This needs to be coordinated with the context type definition in SelectRootContext.tsx.

🤖 Prompt for AI Agents
In src/components/ui/Select/fragments/SelectRoot.tsx at line 14, the context
value passed to SelectRootContext.Provider includes selectedId and
setSelectedId, but these properties are commented out in the
SelectRootContextType interface, causing a type mismatch. To fix this, update
the SelectRootContextType interface in SelectRootContext.tsx to include
selectedId and setSelectedId with the correct types, ensuring the context value
matches the interface definition and resolves the TypeScript errors.

Comment on lines +11 to +12
// selectedId: '',
// setSelectedId: () => {}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Update default context values.

If the properties are uncommented in the type definition, also uncomment and fix the default values:

 export const SelectRootContext = React.createContext<SelectRootContextType>({
     rootClass: '',
-    // selectedId: '',
-    // setSelectedId: () => {}
+    selectedId: null,
+    setSelectedId: () => {}
 });
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// selectedId: '',
// setSelectedId: () => {}
export const SelectRootContext = React.createContext<SelectRootContextType>({
rootClass: '',
selectedId: null,
setSelectedId: () => {}
});
🤖 Prompt for AI Agents
In src/components/ui/Select/contexts/SelectRootContext.tsx at lines 11 to 12,
the default context values for selectedId and setSelectedId are commented out.
Uncomment these lines and provide appropriate default values: set selectedId to
an empty string and setSelectedId to a no-op function to match the type
definition and ensure the context has valid defaults.

Comment on lines +5 to +6
// selectedId: string;
// setSelectedId: React.Dispatch<React.SetStateAction<string>>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Resolve context type inconsistency.

The selectedId and setSelectedId properties are commented out in the type definition but are actively used in SelectRoot.tsx and SelectContent.tsx. This creates TypeScript errors throughout the codebase.

Either uncomment these properties to support the new functionality:

 interface SelectRootContextType {
    rootClass: string;
-//    selectedId: string;
-//    setSelectedId: React.Dispatch<React.SetStateAction<string>>
+    selectedId: number | null;
+    setSelectedId: React.Dispatch<React.SetStateAction<number | null>>
 }

Or remove their usage from other components if this approach is being abandoned.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// selectedId: string;
// setSelectedId: React.Dispatch<React.SetStateAction<string>>
// src/components/ui/Select/contexts/SelectRootContext.tsx
interface SelectRootContextType {
rootClass: string;
selectedId: number | null;
setSelectedId: React.Dispatch<React.SetStateAction<number | null>>;
}
🤖 Prompt for AI Agents
In src/components/ui/Select/contexts/SelectRootContext.tsx around lines 5 to 6,
the selectedId and setSelectedId properties are commented out in the context
type definition but are used in SelectRoot.tsx and SelectContent.tsx, causing
TypeScript errors. To fix this, uncomment these two properties in the type
definition to align with their usage, ensuring type consistency across
components.

Comment on lines 39 to 44
const { refs, floatingStyles, context: floatingContext } = Floater.useFloating({
middleware: [Floater.offset(offsetValue)],
open: isOpen,
onOpenChange: setIsOpen,
placement
placement,
});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Handle undefined offsetValue and optimize middleware array.

The current implementation has two issues:

  1. offsetValue is optional but passed directly to Floater.offset(), which may not handle undefined gracefully
  2. The middleware array is recreated on every render
+    const middleware = React.useMemo(() => {
+        const middlewares = [];
+        if (offsetValue !== undefined) {
+            middlewares.push(Floater.offset(offsetValue));
+        }
+        return middlewares;
+    }, [offsetValue]);
+
     const { refs, floatingStyles, context: floatingContext } = Floater.useFloating({
-        middleware: [Floater.offset(offsetValue)],
+        middleware,
         open: isOpen,
         onOpenChange: setIsOpen,
         placement,
     });
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const { refs, floatingStyles, context: floatingContext } = Floater.useFloating({
middleware: [Floater.offset(offsetValue)],
open: isOpen,
onOpenChange: setIsOpen,
placement
placement,
});
const middleware = React.useMemo(() => {
const middlewares: Floater.Middleware[] = [];
if (offsetValue !== undefined) {
middlewares.push(Floater.offset(offsetValue));
}
return middlewares;
}, [offsetValue]);
const { refs, floatingStyles, context: floatingContext } = Floater.useFloating({
middleware,
open: isOpen,
onOpenChange: setIsOpen,
placement,
});
🤖 Prompt for AI Agents
In src/core/primitives/Select/fragments/SelectPrimitiveRoot.tsx around lines 39
to 44, the offsetValue passed to Floater.offset may be undefined, which can
cause issues, and the middleware array is recreated on every render causing
unnecessary re-renders. Fix this by conditionally including
Floater.offset(offsetValue) in the middleware array only if offsetValue is
defined, and memoize the middleware array using React.useMemo to avoid
recreating it on each render.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

♻️ Duplicate comments (1)
src/core/primitives/Select/fragments/SelectPrimitiveRoot.tsx (1)

44-48: ⚠️ Potential issue

Handle undefined offsetValue and optimize middleware array.

The offsetValue handling issue from the previous review still needs to be addressed.

+    const middleware = React.useMemo(() => {
+        const middlewares = [];
+        if (offsetPositionValue !== undefined) {
+            middlewares.push(Floater.offset(offsetPositionValue));
+        }
+        return middlewares;
+    }, [offsetPositionValue]);
+
     const { refs, floatingStyles, context: floatingContext } = Floater.useFloating({
-        middleware: [Floater.offset(offsetPositionValue)],
+        middleware,
         open: isOpen,
         onOpenChange: setIsOpen,
         placement,
     });
🧹 Nitpick comments (3)
src/core/primitives/Select/fragments/SelectPrimitiveItem.tsx (1)

17-17: Remove trailing whitespace.

-    
+
src/core/primitives/Select/fragments/SelectPrimitiveRoot.tsx (2)

79-80: Extract position calculation for clarity.

The position calculation could be clearer with better variable naming.

-        const position = (selectedIndex) * (floatingElement.clientHeight / floatingElement.children.length);
-        setOffsetPositionValue(-position);
+        const itemHeight = floatingElement.clientHeight / floatingElement.children.length;
+        const offset = selectedIndex * itemHeight;
+        setOffsetPositionValue(-offset);

63-81: Consider edge cases in positioning logic.

The current implementation might have issues with:

  1. Empty children array (division by zero)
  2. Dynamic content that changes the number of items
  3. Items with varying heights

Would you like me to help implement a more robust positioning system that handles these edge cases?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c45bdd6 and e43a674.

📒 Files selected for processing (5)
  • src/components/ui/Select/fragments/SelectContent.tsx (2 hunks)
  • src/components/ui/Select/fragments/SelectRoot.tsx (2 hunks)
  • src/core/primitives/Select/contexts/SelectPrimitiveContext.tsx (1 hunks)
  • src/core/primitives/Select/fragments/SelectPrimitiveItem.tsx (1 hunks)
  • src/core/primitives/Select/fragments/SelectPrimitiveRoot.tsx (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/components/ui/Select/fragments/SelectRoot.tsx
  • src/components/ui/Select/fragments/SelectContent.tsx
🔇 Additional comments (2)
src/core/primitives/Select/fragments/SelectPrimitiveItem.tsx (1)

15-15: LGTM!

The implementation correctly assigns the selectedItemRef only to the selected item, which aligns with the positioning control objective.

Also applies to: 25-25

src/core/primitives/Select/contexts/SelectPrimitiveContext.tsx (1)

20-21: LGTM!

The context type extensions are well-defined and support the positioning control feature.

const index = children.indexOf(selectedItemRef.current);

setSelectedIndex(index + 1);
}, [refs.floating.current, selectedItemRef?.current]);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix dependency array syntax.

The dependency arrays use incorrect syntax with .current. React expects the ref object itself, not the current value.

-    }, [refs.floating.current, selectedItemRef?.current]);
+    }, [refs.floating, selectedItemRef]);

And similarly:

-    }, [isOpen, selectedIndex, refs.floating.current]);
+    }, [isOpen, selectedIndex, refs.floating]);

Also applies to: 81-81

🤖 Prompt for AI Agents
In src/core/primitives/Select/fragments/SelectPrimitiveRoot.tsx at lines 71 and
81, the dependency arrays incorrectly include `.current` from ref objects. React
expects the ref objects themselves as dependencies, not their `.current` values.
Remove `.current` from the dependency arrays so that only the ref objects (e.g.,
refs.floating and selectedItemRef) are included.

const children = Array.from(floatingElement.children);
const index = children.indexOf(selectedItemRef.current);

setSelectedIndex(index + 1);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix incorrect index calculation.

Adding 1 to the index seems incorrect. Array indices start at 0, so this would make the first item have index 1 instead of 0, potentially causing off-by-one errors in the position calculation.

-        setSelectedIndex(index + 1);
+        setSelectedIndex(index);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
setSelectedIndex(index + 1);
setSelectedIndex(index);
🤖 Prompt for AI Agents
In src/core/primitives/Select/fragments/SelectPrimitiveRoot.tsx at line 70, the
code adds 1 to the index when setting selectedIndex, which causes an off-by-one
error. Remove the addition of 1 so that selectedIndex is set directly to the
index value, ensuring correct zero-based indexing.

@kotAPI kotAPI added the automerge A tag that tells kodiak bot to automerge PRs for us when tests and approval conditions are met label Jun 8, 2025
@kodiakhq kodiakhq Bot merged commit 5b8fa24 into main Jun 8, 2025
7 checks passed
@kodiakhq kodiakhq Bot deleted the feat/poc-select-content-postion branch June 8, 2025 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automerge A tag that tells kodiak bot to automerge PRs for us when tests and approval conditions are met

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants