Skip to content

feat: Badges in ion-tabs #4039

Closed
Closed

Description

@donhmorris

Prerequisites

Describe the Feature Request

Would be helpful to specify the display of a badge with a numeric count within a particular ion-tab-button

Describe the Use Case

Show the count of new messages for a tab button that navigates to an inbox page.

Describe Preferred Solution

Add the following properties to ion-tab-button

  • displayBadge: boolean
  • badgeValue: number
  • badgeColor: color

Describe Alternatives

Haven't really discovered anything I like.

Related Code

No response

Additional Information

No response

Activity

  1. EinfachHans commented on Jun 19, 2024

    @EinfachHans

    I just use <ion-badge>, doesn't that work for you?

  2. donhmorris commented on Jun 19, 2024

    @donhmorris
    Author

    I just use <ion-badge>, doesn't that work for you?

    I tried it with the tabs starter project. It doesn't seem to work well. After viewing the result, I get the feeling the designers didn't plan for it because it doesn't look right. The badge is jammed against the icon with no margin or spacing. I can add spacing with css but i don't usually need to do this for badges. They normally just work.

    I think we need to hear from the team the official word on using badges in tab buttons and update the documentation accordingly.

    Screenshot 2024-06-19 at 2 58 09 PM
  3. sean-perkins commented on Jun 20, 2024

    @sean-perkins
    Contributor

    I am no longer on the team, but ion-badge is the correct/intended component for this usage. ion-tab-button has specific styling targeting when an ion-badge is slotted: https://github.com/ionic-team/ionic-framework/blob/ff214bd5f3da947d861d5d0b81575791c391a9b8/core/src/components/tab-button/tab-button.ios.scss#L16-L28

    Since the element is slotted, badge's position can be customized through CSS in the light dom.

  4. donhmorris commented on Jun 20, 2024

    @donhmorris
    Author

    Sean, are you saying that I can choose a slot like 'end' when including a badge? The docs for ion-tab-button and ion-tab say there aren't any slots for these components.

    I tried specifying a slot and the badge disappears.

    `

    Tab 1 5
    <ion-tab-button tab="tab2" href="/tabs/tab2">
      <ion-icon aria-hidden="true" name="ellipse"></ion-icon>
      <ion-label>Tab 2</ion-label>
      <ion-badge color="success">10</ion-badge>
    </ion-tab-button>
    
    <ion-tab-button tab="tab3" href="/tabs/tab3">
      <ion-icon aria-hidden="true" name="square"></ion-icon>
      <ion-label>Tab 3</ion-label>
      <ion-badge slot="end" color="danger">7</ion-badge>
    </ion-tab-button>
    
    ` Screenshot 2024-06-20 at 11 09 47 AM
  5. sean-perkins commented on Jun 20, 2024

    @sean-perkins
    Contributor

    ion-tab-button supports a single/default slot: https://github.com/ionic-team/ionic-framework/blob/ff214bd5f3da947d861d5d0b81575791c391a9b8/core/src/components/tab-button/tab-button.tsx#L179

    e.g.:

    <ion-tab-button>
      Text
      <ion-badge></ion-badge>
    </ion-tab-button>

    The styling shared earlier will target the badge and position it absolutely in the container.

  6. donhmorris commented on Jun 20, 2024

    @donhmorris
    Author

    Ok, but it doesn't work well. The badge gets jammed against the icon. I can use css to move it farther away but it seems like the default slot should obviate that. Seems broken based on the appearance.
    Screenshot 2024-06-20 at 11 20 41 AM

  7. sean-perkins commented on Jun 20, 2024

    @sean-perkins
    Contributor

    Ionicons are not of a consistent viewbox, so you will need to manually position your badge if you want a specific visual appearance.

    Ionic Framework tries to closely resemble the visual appearance of the iOS tab bar: https://developer.apple.com/documentation/swiftui/tabview

    image

  8. donhmorris commented on Jun 20, 2024

    @donhmorris
    Author

    I can live with that.

  9. donhmorris commented on Jun 20, 2024

    @donhmorris
    Author

    I think the docs should be updated for any component that has a 'default' slot. I wasn't aware that such a thing existed nor how it works. Maybe I missed that.

    Probably would be a good idea to update the docs for ion-tabs to cover the use of badges since it is such a common use case and it's not obvious how to do it. I've spoken to other devs who didn't know how it works.

    Thanks for commenting.

  10. thetaPC commented on Mar 13, 2025

    @thetaPC
    Contributor

    Thank you for submitting the issue! Glad to hear that it's been resolved! I'll be moving this issue over to the docs repo though. It would be great to add a section to show how to use badges in the tab bar.

  11. added
    content
    on Mar 13, 2025
  12. thetaPC commented on Mar 18, 2025

    @thetaPC
    Contributor

    The new playgrounds will be on the site very soon! You'll find them under the ion-badge and ion-tab-bar pages.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    content

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions