-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
feat(input-component): support icons left/right #5407
Open
IndyVC
wants to merge
1
commit into
shadcn-ui:main
Choose a base branch
from
IndyVC:feature/icons-input
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+360
−26
Open
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 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
This file contains 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
This file contains 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
This file contains 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,15 @@ | ||
{ | ||
"name": "input-with-icons", | ||
"type": "registry:example", | ||
"registryDependencies": [ | ||
"input" | ||
], | ||
"files": [ | ||
{ | ||
"path": "example/input-with-icons.tsx", | ||
"content": "import { MapIcon, MapPin } from \"lucide-react\"\n\nimport { Input } from \"@/registry/default/ui/input\"\n\nexport default function InputWithIcons() {\n return (\n <div>\n <Input>\n <Input.Icon side=\"left\">\n <MapIcon />\n </Input.Icon>\n <Input.Icon side=\"right\">\n <MapPin />\n </Input.Icon>\n </Input>\n </div>\n )\n}\n", | ||
"type": "registry:example", | ||
"target": "" | ||
} | ||
] | ||
} |
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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,15 @@ | ||
{ | ||
"name": "input-with-icons", | ||
"type": "registry:example", | ||
"registryDependencies": [ | ||
"input" | ||
], | ||
"files": [ | ||
{ | ||
"path": "example/input-with-icons.tsx", | ||
"content": "import { MapIcon, MapPin } from \"lucide-react\"\n\nimport { Input } from \"@/registry/new-york/ui/input\"\n\nexport default function InputWithIcons() {\n return (\n <div>\n <Input>\n <Input.Icon side=\"left\">\n <MapIcon />\n </Input.Icon>\n <Input.Icon side=\"right\">\n <MapPin />\n </Input.Icon>\n </Input>\n </div>\n )\n}\n", | ||
"type": "registry:example", | ||
"target": "" | ||
} | ||
] | ||
} |
This file contains 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
This file contains 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,18 @@ | ||
import { MapIcon, MapPin } from "lucide-react" | ||
|
||
import { Input } from "@/registry/default/ui/input" | ||
|
||
export default function InputWithIcons() { | ||
return ( | ||
<div> | ||
<Input> | ||
<Input.Icon side="left"> | ||
<MapIcon /> | ||
</Input.Icon> | ||
<Input.Icon side="right"> | ||
<MapPin /> | ||
</Input.Icon> | ||
</Input> | ||
</div> | ||
) | ||
} |
This file contains 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,29 @@ | ||
import * as React from "react" | ||
|
||
type ReactElementWithDisplayName = React.ReactElement & { | ||
type: { displayName: string } | ||
} | ||
|
||
const isElement = ( | ||
element: unknown | undefined | ||
): element is ReactElementWithDisplayName => { | ||
return ( | ||
(element as ReactElementWithDisplayName)?.type?.displayName !== undefined | ||
) | ||
} | ||
|
||
export function useComposition( | ||
children: React.ReactNode, | ||
component: string | undefined | ||
) { | ||
const Children = React.useMemo( | ||
() => | ||
React.Children.toArray(children).filter((child) => { | ||
if (isElement(child)) return child.type.displayName === component | ||
return false | ||
}), | ||
[children, component] | ||
) | ||
|
||
return Children | ||
} |
This file contains 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 |
---|---|---|
@@ -1,22 +1,103 @@ | ||
import * as React from "react" | ||
import { Slot } from "@radix-ui/react-slot" | ||
import { cva, type VariantProps } from "class-variance-authority" | ||
|
||
import { cn } from "@/lib/utils" | ||
import { useComposition } from "@/registry/default/hooks/use-composition" | ||
|
||
const Input = React.forwardRef<HTMLInputElement, React.ComponentProps<"input">>( | ||
interface InputComposition { | ||
Icon: typeof InputIcon | ||
} | ||
|
||
const iconVariants = cva("absolute top-3", { | ||
variants: { | ||
size: { | ||
default: "h-4 w-4", | ||
}, | ||
side: { | ||
left: "left-3", | ||
right: "right-3", | ||
}, | ||
}, | ||
defaultVariants: { | ||
size: "default", | ||
side: "left", | ||
}, | ||
}) | ||
|
||
export interface InputIconProps | ||
extends React.HTMLAttributes<HTMLOrSVGElement>, | ||
VariantProps<typeof iconVariants> {} | ||
|
||
const InputIcon = React.forwardRef<HTMLSlotElement, InputIconProps>( | ||
({ children, className, size, side }, ref) => { | ||
return ( | ||
<Slot | ||
data-icon | ||
ref={ref} | ||
className={cn(iconVariants({ size, side }), className)} | ||
> | ||
{children} | ||
</Slot> | ||
) | ||
} | ||
) | ||
InputIcon.displayName = "InputIcon" | ||
|
||
const inputVariants = cva( | ||
"flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-base ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 md:text-sm", | ||
{ | ||
variants: { | ||
composition: { | ||
true: "px-10", | ||
false: "px-3", | ||
}, | ||
}, | ||
defaultVariants: { | ||
composition: false, | ||
}, | ||
} | ||
) | ||
export interface InputProps | ||
extends React.InputHTMLAttributes<HTMLInputElement> {} | ||
|
||
const Input = React.forwardRef<HTMLInputElement, InputProps>( | ||
({ className, type, ...props }, ref) => { | ||
return <input type={type} className={className} ref={ref} {...props} /> | ||
} | ||
) | ||
Input.displayName = "Input" | ||
|
||
const Root = React.forwardRef<HTMLInputElement, InputProps>( | ||
({ children, className, ...props }, ref) => { | ||
const Icons = useComposition(children, InputIcon.displayName) | ||
|
||
if (Icons.length > 0) { | ||
return ( | ||
<div className="relative"> | ||
{Icons} | ||
<Input | ||
ref={ref} | ||
className={cn(inputVariants({ composition: true }), className)} | ||
{...props} | ||
/> | ||
</div> | ||
) | ||
} | ||
return ( | ||
<input | ||
type={type} | ||
className={cn( | ||
"flex h-10 w-full rounded-md border border-input bg-background px-3 py-2 text-base ring-offset-background file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50 md:text-sm", | ||
className | ||
)} | ||
<Input | ||
ref={ref} | ||
className={cn(inputVariants({ composition: false }), className)} | ||
{...props} | ||
/> | ||
) | ||
} | ||
) | ||
Input.displayName = "Input" | ||
) as React.ForwardRefExoticComponent< | ||
InputProps & React.RefAttributes<HTMLInputElement> | ||
> & | ||
InputComposition | ||
|
||
Root.displayName = "Input" | ||
Root.Icon = InputIcon | ||
|
||
export { Input } | ||
export { Root as Input } |
This file contains 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,18 @@ | ||
import { MapIcon, MapPin } from "lucide-react" | ||
|
||
import { Input } from "@/registry/new-york/ui/input" | ||
|
||
export default function InputWithIcons() { | ||
return ( | ||
<div> | ||
<Input> | ||
<Input.Icon side="left"> | ||
<MapIcon /> | ||
</Input.Icon> | ||
<Input.Icon side="right"> | ||
<MapPin /> | ||
</Input.Icon> | ||
</Input> | ||
</div> | ||
) | ||
} |
This file contains 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,26 @@ | ||
import * as React from "react" | ||
|
||
type ReactElementWithDisplayName = React.ReactElement & { | ||
type: { displayName: string } | ||
} | ||
|
||
const isElement = ( | ||
element: unknown | undefined | ||
): element is ReactElementWithDisplayName => { | ||
return ( | ||
(element as ReactElementWithDisplayName)?.type?.displayName !== undefined | ||
) | ||
} | ||
|
||
export function useComposition(children: React.ReactNode, component: string) { | ||
const Child = React.useMemo( | ||
() => | ||
React.Children.toArray(children).find((child) => { | ||
if (isElement(child)) return child.type.displayName === component | ||
return false | ||
}), | ||
[children, component] | ||
) | ||
|
||
return Child ?? null | ||
} |
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.
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.
I’ve noticed one issue with composition. For my use case and most of the time when building other input components I’ve been playing with the
padding-x
property only on the side where the icon is added. This way, there’s no extra space on both sides from only one icon being present.Something like this is what I thought could do the job:
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.
@florinkrasniqi I don't fully understand the comment:
do you mean I should be using
px-3
instead ofleft-3
for example? (seeiconVariants
)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.
Sorry for any confusion. The whole idea is to not have input padding on both sides if only one icon is present but instead only on the icon side.