-
Notifications
You must be signed in to change notification settings - Fork 13.4k
feat(toolbar): add titlePlacement prop to control title position relative to content #31034
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
base: next
Are you sure you want to change the base?
Changes from all commits
32d5e4e
e9480e9
77583dd
d2ae878
eac4475
4421028
9060abf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en" dir="ltr"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <title>Toolbar - Title Placement</title> | ||
| <meta | ||
| name="viewport" | ||
| content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" | ||
| /> | ||
| <link href="../../../../../css/ionic.bundle.css" rel="stylesheet" /> | ||
| <link href="../../../../../scripts/testing/styles.css" rel="stylesheet" /> | ||
| <script src="../../../../../scripts/testing/scripts.js"></script> | ||
| <script nomodule src="../../../../../dist/ionic/ionic.js"></script> | ||
| <script type="module" src="../../../../../dist/ionic/ionic.esm.js"></script> | ||
| </head> | ||
|
|
||
| <body> | ||
| <ion-app> | ||
| <ion-header id="default-placement"> | ||
| <ion-toolbar> | ||
| <ion-title>Default Placement</ion-title> | ||
| </ion-toolbar> | ||
| <ion-toolbar> | ||
| <ion-buttons slot="start"> | ||
| <ion-button> | ||
| <ion-icon slot="icon-only" name="menu"></ion-icon> | ||
| </ion-button> | ||
| </ion-buttons> | ||
| <ion-title>Default Placement</ion-title> | ||
| </ion-toolbar> | ||
| <ion-toolbar> | ||
| <ion-title>Default Placement</ion-title> | ||
| <ion-buttons slot="primary"> | ||
| <ion-button> | ||
| <ion-icon slot="icon-only" name="search"></ion-icon> | ||
| </ion-button> | ||
| </ion-buttons> | ||
| </ion-toolbar> | ||
| </ion-header> | ||
| <ion-header> | ||
| <ion-toolbar title-placement="start"> | ||
| <ion-title>Start Placement</ion-title> | ||
| </ion-toolbar> | ||
| <ion-toolbar title-placement="start"> | ||
| <ion-buttons slot="start"> | ||
| <ion-button> | ||
| <ion-icon slot="icon-only" name="menu"></ion-icon> | ||
| </ion-button> | ||
| </ion-buttons> | ||
| <ion-title>Start Placement</ion-title> | ||
| </ion-toolbar> | ||
| <ion-toolbar title-placement="start"> | ||
| <ion-title>Start Placement</ion-title> | ||
| <ion-buttons slot="primary"> | ||
| <ion-button> | ||
| <ion-icon slot="icon-only" name="search"></ion-icon> | ||
| </ion-button> | ||
| </ion-buttons> | ||
| </ion-toolbar> | ||
| </ion-header> | ||
| <ion-header> | ||
| <ion-toolbar title-placement="center"> | ||
| <ion-title>Center Placement</ion-title> | ||
| </ion-toolbar> | ||
| <ion-toolbar title-placement="center"> | ||
| <ion-buttons slot="start"> | ||
| <ion-button> | ||
| <ion-icon slot="icon-only" name="menu"></ion-icon> | ||
| </ion-button> | ||
| </ion-buttons> | ||
| <ion-title>Center Placement</ion-title> | ||
| </ion-toolbar> | ||
| <ion-toolbar title-placement="center"> | ||
| <ion-title>Center Placement</ion-title> | ||
| <ion-buttons slot="primary"> | ||
| <ion-button> | ||
| <ion-icon slot="icon-only" name="search"></ion-icon> | ||
| </ion-button> | ||
| </ion-buttons> | ||
| </ion-toolbar> | ||
| </ion-header> | ||
| <ion-header> | ||
| <ion-toolbar title-placement="end"> | ||
| <ion-title>End Placement</ion-title> | ||
| </ion-toolbar> | ||
| <ion-toolbar title-placement="end"> | ||
| <ion-buttons slot="start"> | ||
| <ion-button> | ||
| <ion-icon slot="icon-only" name="menu"></ion-icon> | ||
| </ion-button> | ||
| </ion-buttons> | ||
| <ion-title>End Placement</ion-title> | ||
| </ion-toolbar> | ||
| <ion-toolbar title-placement="end"> | ||
| <ion-title>End Placement</ion-title> | ||
| <ion-buttons slot="primary"> | ||
| <ion-button> | ||
| <ion-icon slot="icon-only" name="search"></ion-icon> | ||
| </ion-button> | ||
| </ion-buttons> | ||
| </ion-toolbar> | ||
| </ion-header> | ||
|
|
||
| <ion-footer> | ||
| <ion-button onclick="toggleTitlePlacement('start')">Start</ion-button> | ||
| <ion-button onclick="toggleTitlePlacement('center')">Center</ion-button> | ||
| <ion-button onclick="toggleTitlePlacement('end')">End</ion-button> | ||
| </ion-footer> | ||
| </ion-app> | ||
|
|
||
| <script> | ||
| const defaultPlacementToolbars = document.querySelectorAll('#default-placement ion-toolbar'); | ||
|
|
||
| function toggleTitlePlacement(placement) { | ||
| defaultPlacementToolbars.forEach((toolbar) => { | ||
| toolbar.titlePlacement = placement; | ||
| }); | ||
| } | ||
| </script> | ||
|
|
||
| <style> | ||
| ion-footer { | ||
| display: flex; | ||
|
|
||
| justify-content: center; | ||
| align-items: center; | ||
|
|
||
| gap: 10px; | ||
| padding: 8px; | ||
| } | ||
| </style> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { expect } from '@playwright/test'; | ||
| import { configs, test } from '@utils/test/playwright'; | ||
|
|
||
| /** | ||
| * Title placement is only supported by the `ionic` theme. | ||
| */ | ||
| configs({ modes: ['ionic-md'] }).forEach(({ title, screenshot, config }) => { | ||
| test.describe(title('toolbar: title-placement'), () => { | ||
| ['start', 'center', 'end'].forEach((placement) => { | ||
| test(`should not have visual regressions with ${placement} placement`, async ({ page }) => { | ||
| const placementTitle = placement.charAt(0).toUpperCase() + placement.slice(1); | ||
|
|
||
| await page.setContent( | ||
| ` | ||
| <ion-header> | ||
| <ion-toolbar title-placement="${placement}"> | ||
| <ion-title>${placementTitle} Placement</ion-title> | ||
| </ion-toolbar> | ||
| <ion-toolbar title-placement="${placement}"> | ||
| <ion-buttons slot="start"> | ||
| <ion-button> | ||
| <ion-icon slot="icon-only" name="menu"></ion-icon> | ||
| </ion-button> | ||
| </ion-buttons> | ||
| <ion-title>${placementTitle} Placement</ion-title> | ||
| </ion-toolbar> | ||
| <ion-toolbar title-placement="${placement}"> | ||
| <ion-title>${placementTitle} Placement</ion-title> | ||
| <ion-buttons slot="primary"> | ||
| <ion-button> | ||
| <ion-icon slot="icon-only" name="search"></ion-icon> | ||
| </ion-button> | ||
| </ion-buttons> | ||
| </ion-toolbar> | ||
| </ion-header> | ||
| `, | ||
| config | ||
| ); | ||
|
|
||
| const header = page.locator('ion-header'); | ||
| await expect(header).toHaveScreenshot(screenshot(`toolbar-title-placement-${placement}`)); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import type { ComponentInterface } from '@stencil/core'; | ||
| import { Component, Element, forceUpdate, h, Host, Listen, Prop } from '@stencil/core'; | ||
| import { Component, Element, forceUpdate, h, Host, Listen, Prop, Watch } from '@stencil/core'; | ||
| import { createColorClasses, hostContext } from '@utils/theme'; | ||
|
|
||
| import { getIonTheme } from '../../global/ionic-global'; | ||
|
|
@@ -30,6 +30,14 @@ import type { Color, CssClassMap, StyleEventDetail } from '../../interface'; | |
| }) | ||
| export class Toolbar implements ComponentInterface { | ||
| private childrenStyles = new Map<string, CssClassMap>(); | ||
| private readonly slotClasses = [ | ||
| 'has-start-content', | ||
| 'has-end-content', | ||
| 'has-primary-content', | ||
| 'has-secondary-content', | ||
| ]; | ||
| private readonly showClasses = ['show-start', 'show-end', 'show-primary', 'show-secondary']; | ||
| private readonly slotSizeVars = ['--start-end-size', '--primary-secondary-size']; | ||
|
|
||
| @Element() el!: HTMLIonToolbarElement; | ||
|
|
||
|
|
@@ -40,6 +48,14 @@ export class Toolbar implements ComponentInterface { | |
| */ | ||
| @Prop({ reflect: true }) color?: Color; | ||
|
|
||
| /** | ||
| * Where to place the title relative to the other toolbar content. | ||
| * `"start"`: The title will appear to the left of the toolbar content in LTR and to the right in RTL. | ||
| * `"center"`: The title will appear in the center of the toolbar. | ||
| * `"end"`: The title will appear to the right of the toolbar content in LTR and to the left in RTL. | ||
| */ | ||
| @Prop() titlePlacement?: 'start' | 'center' | 'end'; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I chose to add
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the JSDoc need a note that this currently only has a visual effect in the |
||
|
|
||
| componentWillLoad() { | ||
| const buttons = Array.from(this.el.querySelectorAll('ion-buttons')); | ||
|
|
||
|
|
@@ -67,6 +83,24 @@ export class Toolbar implements ComponentInterface { | |
| this.updateSlotWidths(); | ||
| } | ||
|
|
||
| @Watch('titlePlacement') | ||
| titlePlacementChanged() { | ||
| this.updateSlotClasses(); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the title placement. | ||
| * Returns the title placement if it is set, otherwise returns `"center"` | ||
| * for `ionic` and `ios`, and `"start"` for `md`. | ||
| */ | ||
| private getTitlePlacement(): 'start' | 'center' | 'end' { | ||
| if (this.titlePlacement !== undefined) { | ||
| return this.titlePlacement; | ||
| } | ||
|
|
||
| return getIonTheme(this) === 'ionic' || getIonTheme(this) === 'ios' ? 'center' : 'start'; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While this property doesn't work with |
||
| } | ||
|
|
||
| /** | ||
| * Updates the CSS custom properties for slot widths | ||
| * This ensures that slots shown by their met conditions | ||
|
|
@@ -164,7 +198,21 @@ export class Toolbar implements ComponentInterface { | |
| return allMeasurementsSuccessful; | ||
| } | ||
|
|
||
| /** | ||
| * Removes all slot visibility classes and slot width CSS variables. | ||
| */ | ||
| private removeSlotClasses() { | ||
| this.el.classList.remove(...this.slotClasses, ...this.showClasses); | ||
| this.slotSizeVars.forEach((cssVar) => this.el.style.removeProperty(cssVar)); | ||
| } | ||
|
|
||
| private updateSlotClasses() { | ||
| const titlePlacement = this.getTitlePlacement(); | ||
| if (titlePlacement !== 'center') { | ||
| this.removeSlotClasses(); | ||
| return; | ||
| } | ||
|
|
||
| // Check if slots have content | ||
| const slots = ['start', 'end', 'primary', 'secondary']; | ||
|
|
||
|
|
@@ -250,12 +298,15 @@ export class Toolbar implements ComponentInterface { | |
| Object.assign(childStyles, style); | ||
| }); | ||
|
|
||
| const titlePlacement = this.getTitlePlacement(); | ||
|
|
||
| return ( | ||
| <Host | ||
| class={{ | ||
| ...createColorClasses(this.color, { | ||
| [theme]: true, | ||
| 'in-toolbar': hostContext('ion-toolbar', this.el), | ||
| [`toolbar-title-placement-${titlePlacement}`]: true, | ||
| }), | ||
| ...childStyles, | ||
| }} | ||
|
|
||
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 think this fixed a bug. The only reason the title wasn't centered in the two cases where it was left-aligned is because it had
size="small"on it. I don't think this should change the alignment but I can change it back if desired.