ionic-team / ionic-framework

A powerful cross-platform UI toolkit for building native-quality iOS, Android, and Progressive Web Apps with HTML, CSS, and JavaScript.
https://ionicframework.com
MIT License
50.66k stars 13.52k forks source link

feat: Badges in ion-tabs #29639

Open donhmorris opened 2 weeks ago

donhmorris commented 2 weeks ago

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

Describe Alternatives

Haven't really discovered anything I like.

Related Code

No response

Additional Information

No response

EinfachHans commented 2 weeks ago

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

donhmorris commented 2 weeks ago

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
sean-perkins commented 2 weeks ago

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.

donhmorris commented 2 weeks ago

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 Tab 2 10 Tab 3 7

`

Screenshot 2024-06-20 at 11 09 47 AM
sean-perkins commented 2 weeks ago

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.

donhmorris commented 2 weeks ago

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
sean-perkins commented 2 weeks ago

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

donhmorris commented 2 weeks ago

I can live with that.

donhmorris commented 2 weeks ago

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.