Skip to content
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): adding outside-top prop #4775

Open
wants to merge 6 commits into
base: canary
Choose a base branch
from

Conversation

abhinav700
Copy link
Contributor

@abhinav700 abhinav700 commented Feb 1, 2025

This PR is duplicate of #3660
PR #4761 is closed because the old branch is deleted

📝 Description

⛳️ Current behavior (updates)

In old behaviour, the label was presented inside the input component if placholder === "" even if labelPlacement === "outside"

🚀 New behavior

Adding outside-top prop which dispalys the label outside the input component regardless of whether the placeholder is there or not.

💣 Is this a breaking change (Yes/No):

📝 Additional Information

image

Summary by CodeRabbit

  • New Features
    • Introduced a new "outside-top" label placement option for input components, allowing labels to appear above the input field.
  • Documentation
    • Updated content and examples to clearly explain the new label placement behavior and its visual impact in the user interface.

Copy link

changeset-bot bot commented Feb 1, 2025

⚠️ No Changeset found

Latest commit: bdc94f8

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

vercel bot commented Feb 1, 2025

@abhinav700 is attempting to deploy a commit to the HeroUI Inc Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

coderabbitai bot commented Feb 1, 2025

Walkthrough

This pull request introduces a new label placement option, "outside-top," for the Input component. The changes update both the component’s logic and its accompanying documentation. In the component’s code, a new property (isOutsideTop) is added to control label rendering, and the useInput hook’s evaluation logic is modified accordingly. Storybook examples now include the new placement, and theme configurations are updated to support the additional layout style. No public API declarations have been altered.

Changes

File(s) Summary
apps/docs/.../input/label-placements.raw.jsx
apps/docs/.../docs/input.mdx
Updated documentation to include the "outside-top" option and clarify label rendering behavior based on labelPlacement settings.
packages/components/.../input.tsx Introduced a new property (isOutsideTop) and updated label rendering conditions to check both isOutsideLeft and isOutsideTop.
packages/components/.../use-input.ts Replaced the useLabelPlacement hook with useMemo logic; added isOutsideTop and isOutsideLeft evaluations; updated the shouldLabelBeOutside condition and issued a warning for unsupported cases.
packages/components/.../input.stories.tsx Expanded the labelPlacement options in the Storybook argTypes to include "outside-top" and added a demonstration instance for the new option.
packages/core/.../input.ts Added a new variant for labelPlacement ("outside-top") in the theme configuration with updated styling rules.

Sequence Diagram(s)

sequenceDiagram
    participant IC as Input Component
    participant UIH as useInput Hook
    participant R as Rendering Logic

    IC->>UIH: Call useInput(labelPlacement, placeholder, ...)
    UIH->>UIH: Evaluate conditions for isOutsideLeft and isOutsideTop
    UIH-->>IC: Return { isOutsideLeft, isOutsideTop, shouldLabelBeOutside }
    IC->>R: Render label outside if (isOutsideLeft OR isOutsideTop) else render inside
Loading
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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. (Beta)
  • @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.

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.

@abhinav700
Copy link
Contributor Author

@wingkwong I have created a new branch and added the changes there

Copy link
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: 0

🧹 Nitpick comments (1)
apps/docs/content/docs/components/input.mdx (1)

84-87: Consider rephrasing the notes to improve readability.

The notes provide valuable clarification about label placement behavior, but they could be more concise and avoid repetition.

Consider this revision:

-> **Note**: If the `labelPlacement` is `outside`, `label` is outside only when a placeholder is provided. 
-
-> **Note**: If the `labelPlacement` is `outside-top` or `outside-left`, `label` is outside even if a placeholder is not provided.
+> **Note**: Label placement behavior varies:
+> - With `outside`: Label appears outside only when a placeholder is provided.
+> - With `outside-top` or `outside-left`: Label always appears outside, regardless of placeholder.
🧰 Tools
🪛 LanguageTool

[style] ~86-~86: Three successive sentences begin with the same word. Consider rewording the sentence or use a thesaurus to find a synonym.
Context: ...y when a placeholder is provided. > Note: If the labelPlacement is `outside-...

(ENGLISH_WORD_REPEAT_BEGINNING_RULE)

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 92281a6 and 6b6ae46.

📒 Files selected for processing (6)
  • apps/docs/content/components/input/label-placements.raw.jsx (1 hunks)
  • apps/docs/content/docs/components/input.mdx (1 hunks)
  • packages/components/input/src/input.tsx (3 hunks)
  • packages/components/input/src/use-input.ts (2 hunks)
  • packages/components/input/stories/input.stories.tsx (3 hunks)
  • packages/core/theme/src/components/input.ts (1 hunks)
🧰 Additional context used
🪛 LanguageTool
apps/docs/content/docs/components/input.mdx

[style] ~86-~86: Three successive sentences begin with the same word. Consider rewording the sentence or use a thesaurus to find a synonym.
Context: ...y when a placeholder is provided. > Note: If the labelPlacement is `outside-...

(ENGLISH_WORD_REPEAT_BEGINNING_RULE)

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: TypeScript
  • GitHub Check: Build
🔇 Additional comments (6)
apps/docs/content/components/input/label-placements.raw.jsx (1)

4-4: LGTM!

The addition of "outside-top" to the placements array is consistent with the new feature.

packages/components/input/src/input.tsx (1)

20-20: LGTM!

The implementation correctly handles the new "outside-top" label placement by:

  1. Adding the isOutsideTop prop
  2. Updating the label rendering conditions to consider the new placement

Also applies to: 86-86, 119-119

packages/components/input/src/use-input.ts (1)

242-244: LGTM!

The implementation correctly:

  1. Adds variables to track label placement
  2. Updates the logic to handle the new "outside-top" placement
  3. Maintains clear documentation of the behavior

Also applies to: 245-250, 260-261

packages/components/input/stories/input.stories.tsx (1)

53-53: LGTM!

The changes correctly:

  1. Add the new placement option to the Storybook configuration
  2. Include comprehensive examples demonstrating the feature with and without placeholders

Also applies to: 178-182, 187-206

packages/core/theme/src/components/input.ts (1)

181-186: LGTM! Clean implementation of the "outside-top" label placement.

The implementation follows the established pattern and uses appropriate Tailwind classes for layout and spacing.

apps/docs/content/docs/components/input.mdx (1)

78-78: LGTM! Clear documentation of the new option.

The documentation accurately includes the new "outside-top" option in the list of available label placements.

Copy link
Member

@wingkwong wingkwong left a comment

Choose a reason for hiding this comment

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

build & typecheck failed. please check.

@abhinav700
Copy link
Contributor Author

build & typecheck failed. please check.

Searched, but everything seams fine.
labelPlacement is correctly having the type outside || outside-top || outside-left || inside || undefined.

I think this was a common problem in my previous PR also where I would see this error locally but not while making a PR.

Copy link
Member

@wingkwong wingkwong left a comment

Choose a reason for hiding this comment

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

@abhinav700 your local branch is outdated. I just push the latest canary branch to your branch. Just FYI, we did some refactoring on label placement recently. Please also update this file and revise your current change if necessary. Thanks.

https://github.com/heroui-inc/heroui/blob/canary/packages/core/system/src/hooks/use-label-placement.ts

Copy link
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: 0

🧹 Nitpick comments (1)
apps/docs/content/docs/components/input.mdx (1)

86-86: Style Improvement Suggestion for outside-top/outside-left Note
This note states that when labelPlacement is outside-top or outside-left, the label will be displayed outside regardless of a placeholder. However, three successive sentences in this section begin similarly. Consider rephrasing this note to vary the sentence structure and enhance readability.

🧰 Tools
🪛 LanguageTool

[style] ~86-~86: Three successive sentences begin with the same word. Consider rewording the sentence or use a thesaurus to find a synonym.
Context: ...y when a placeholder is provided. > Note: If the labelPlacement is `outside-...

(ENGLISH_WORD_REPEAT_BEGINNING_RULE)

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6b6ae46 and e91d0e3.

📒 Files selected for processing (3)
  • apps/docs/content/docs/components/input.mdx (1 hunks)
  • packages/components/input/src/use-input.ts (2 hunks)
  • packages/core/theme/src/components/input.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/core/theme/src/components/input.ts
  • packages/components/input/src/use-input.ts
🧰 Additional context used
🪛 LanguageTool
apps/docs/content/docs/components/input.mdx

[style] ~86-~86: Three successive sentences begin with the same word. Consider rewording the sentence or use a thesaurus to find a synonym.
Context: ...y when a placeholder is provided. > Note: If the labelPlacement is `outside-...

(ENGLISH_WORD_REPEAT_BEGINNING_RULE)

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: TypeScript
  • GitHub Check: Build
🔇 Additional comments (2)
apps/docs/content/docs/components/input.mdx (2)

78-78: New outside-top Option Introduced
The updated text now includes the outside-top option along with the existing options. This aligns well with the new feature behavior.


84-84: Clarification on outside Label Behavior
This note clarifies that when labelPlacement is set to outside, the label will only be rendered outside if a placeholder is provided. Ensure that the implementation reflects this conditional logic.

Copy link
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: 0

🧹 Nitpick comments (1)
packages/core/system/src/hooks/use-label-placement.ts (1)

12-20: Consider documenting the label placement behavior.

The logic handles the new "outside-top" option correctly. Consider adding JSDoc comments to document the behavior of each placement option for better maintainability.

Add documentation above the function:

+/**
+ * Hook to manage label placement with support for inside, outside, outside-left, and outside-top positions.
+ * Falls back to "outside" when label is not provided with "inside" placement.
+ * @param props.labelPlacement - Desired label placement
+ * @param props.label - Label content
+ * @returns The resolved label placement
+ */
 export function useLabelPlacement(props: {
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e91d0e3 and 190341c.

📒 Files selected for processing (1)
  • packages/core/system/src/hooks/use-label-placement.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: TypeScript
  • GitHub Check: Build
🔇 Additional comments (1)
packages/core/system/src/hooks/use-label-placement.ts (1)

6-6: LGTM! Verify integration with dependent components.

The addition of "outside-top" to the label placement options is a clean, non-breaking change.

Let's verify the integration with dependent components:

✅ Verification successful

🏁 Script executed:

#!/bin/bash
# Search for components using labelPlacement
ast-grep --pattern 'labelPlacement={$_}'

Length of output: 42


🏁 Script executed:

#!/bin/bash
# Let's search for the new "outside-top" option and also check for hook usage throughout the codebase.
echo "Searching for 'outside-top' usage (string literal)..."
rg "\"outside-top\"" .

echo "Searching for 'useLabelPlacement' hook usage..."
rg "useLabelPlacement" .

Length of output: 2470


Integration Verified – "outside-top" Option Properly Propagated

We've confirmed through codebase analysis that the new "outside-top" option is integrated across multiple dependent components (e.g., Input, DateInput, DatePicker, Select) and is properly handled in both hook usage and stories. No issues were found with the hook's implementation or its integration.

@abhinav700
Copy link
Contributor Author

@wingkwong looks like adding outside top to this causes problems in other packages like date-input
should I make changes to the files of these components?

Copy link
Member

@wingkwong wingkwong left a comment

Choose a reason for hiding this comment

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

please add changeset

packages/core/system/src/hooks/use-label-placement.ts Outdated Show resolved Hide resolved
Copy link
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: 0

🧹 Nitpick comments (1)
packages/components/input/src/use-input.ts (1)

267-272: Consider simplifying the isLabelOutside condition.

The condition can be made more readable by grouping related checks.

-  const isLabelOutside = shouldLabelBeOutside
-    ? isOutsideLeft ||
-      isOutsideTop ||
-      hasPlaceholder ||
-      (labelPlacement === "outside" && hasStartContent)
-    : false;
+  const isLabelOutside = shouldLabelBeOutside
+    ? (isOutsideLeft || isOutsideTop) ||  // Always outside
+      (hasPlaceholder || (labelPlacement === "outside" && hasStartContent))  // Conditionally outside
+    : false;
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 190341c and 66d9101.

📒 Files selected for processing (1)
  • packages/components/input/src/use-input.ts (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: TypeScript
  • GitHub Check: Build
  • GitHub Check: Continuous Release
🔇 Additional comments (3)
packages/components/input/src/use-input.ts (3)

250-252: LGTM! Clear and descriptive variable names.

The new variables isOutsideLeft and isOutsideTop follow the existing code style and are well-placed in the code.


253-259: LGTM! Well-documented label placement logic.

The updated shouldLabelBeOutside logic clearly handles both placeholder-dependent and independent cases, matching the PR objectives.


543-543: LGTM! Consistent return object update.

The addition of isOutsideTop to the return object follows the existing pattern and is required for component rendering.

@abhinav700 abhinav700 requested a review from wingkwong February 8, 2025 13:44
Copy link
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: 0

🧹 Nitpick comments (1)
packages/components/input/src/use-input.ts (1)

231-261: Implementation looks good, but consider improving error handling.

The implementation correctly handles file input edge cases and provides appropriate warnings. However, consider adding validation for other unsupported label placements.

   const labelPlacement = useMemo<InputVariantProps["labelPlacement"]>(() => {
+    const supportedPlacements = ["inside", "outside", "outside-left", "outside-top"];
+    if (originalProps.labelPlacement && !supportedPlacements.includes(originalProps.labelPlacement)) {
+      warn(`Unsupported label placement "${originalProps.labelPlacement}". Using default...`);
+      return "inside";
+    }
     if (isFileTypeInput) {
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 66d9101 and bdc94f8.

📒 Files selected for processing (1)
  • packages/components/input/src/use-input.ts (5 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: TypeScript
  • GitHub Check: Build
  • GitHub Check: Continuous Release
🔇 Additional comments (3)
packages/components/input/src/use-input.ts (3)

272-280: LGTM! Clear and concise implementation of label placement logic.

The new variables and logic for determining label placement are well-structured and easy to understand.


564-565: Ensure the new property is properly exposed.

The isOutsideTop property is correctly added to the hook's return value, making it available for consumers.


289-294: Verify the impact on existing label placement behavior.

The modified label outside check includes the new outside-top option. Please ensure this change doesn't affect existing components that rely on the label placement logic.

✅ Verification successful

🏁 Script executed:

#!/bin/bash
# Search for components using label placement
ast-grep --pattern 'labelPlacement={$_}'

Length of output: 42


🏁 Script executed:

#!/bin/bash
# Search for all occurrences of "labelPlacement" in the repository to understand its usage
rg "labelPlacement" -n

Length of output: 34123


Label Placement Behavior Verified for "outside-top"

Based on our review and the repository search results:
• The "outside-top" option is explicitly used in component stories and documented in the examples.
• The new check in use-input.ts now correctly includes a condition for "outside-top" without interfering with the legacy "outside" or "outside-left" behaviors.
• All usages in the codebase align with the intended label placement logic, so no adverse impact on existing components is expected.

Copy link

vercel bot commented Feb 9, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
heroui ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 9, 2025 10:19am
heroui-sb ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 9, 2025 10:19am

Copy link
Member

@wingkwong wingkwong left a comment

Choose a reason for hiding this comment

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

  1. please add changeset
  2. check this case. The labels are somehow center.
    image
  3. Clicking label in outside-top example doesn't focus input. You may check others in Label Placement storybook.

@@ -82,7 +83,7 @@ const Input = forwardRef<"input", InputProps>((props, ref) => {
return (
<div {...getMainWrapperProps()}>
<div {...getInputWrapperProps()}>
{!isOutsideLeft ? labelContent : null}
{!isOutsideLeft && !isOutsideTop ? labelContent : null!}
Copy link
Member

Choose a reason for hiding this comment

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

why null!?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

maybe added by mistake. I don't remember adding it

Comment on lines +231 to +242
/**
* Following code is not used because adding 'outside-top' to
* 'use-label-placement' hook breaks other component.
* Folowing code is replaced with the old way of computing
* 'labelPlacement' for 'input' component.
*
* const labelPlacement = useLabelPlacement({
* labelPlacement: originalProps.labelPlacement,
* label,
* });
*/

Copy link
Member

Choose a reason for hiding this comment

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

// TODO: not using useLabelPlacement because `outside-top` doesn't support in other components.

Comment on lines +244 to +255
if (isFileTypeInput) {
// if `labelPlacement` is not defined, choose `outside` instead
// since the default value `inside` is not supported in file input
if (!originalProps.labelPlacement) return "outside";
// throw a warning if `labelPlacement` is `inside`
// and change it to `outside`
if (originalProps.labelPlacement === "inside") {
warn("Input with file type doesn't support inside label. Converting to outside ...");

return "outside";
}
}
Copy link
Member

Choose a reason for hiding this comment

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

These lines have been deleted in latest canary.

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

Successfully merging this pull request may close these issues.

2 participants