Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2754,6 +2754,7 @@ ion-toolbar,shadow
ion-toolbar,prop,color,"danger" | "dark" | "light" | "medium" | "primary" | "secondary" | "success" | "tertiary" | "warning" | string & Record<never, never> | undefined,undefined,false,true
ion-toolbar,prop,mode,"ios" | "md",undefined,false,false
ion-toolbar,prop,theme,"ios" | "md" | "ionic",undefined,false,false
ion-toolbar,prop,titlePlacement,"center" | "end" | "start" | undefined,undefined,false,false
ion-toolbar,css-prop,--background,ionic
ion-toolbar,css-prop,--background,ios
ion-toolbar,css-prop,--background,md
Expand Down
9 changes: 9 additions & 0 deletions core/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4463,6 +4463,10 @@ export namespace Components {
* The theme determines the visual appearance of the component.
*/
"theme"?: "ios" | "md" | "ionic";
/**
* 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.
*/
"titlePlacement"?: 'start' | 'center' | 'end';
}
}
export interface IonAccordionGroupCustomEvent<T> extends CustomEvent<T> {
Expand Down Expand Up @@ -10590,6 +10594,10 @@ declare namespace LocalJSX {
* The theme determines the visual appearance of the component.
*/
"theme"?: "ios" | "md" | "ionic";
/**
* 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.
*/
"titlePlacement"?: 'start' | 'center' | 'end';
}

interface IonAccordionAttributes {
Expand Down Expand Up @@ -11382,6 +11390,7 @@ declare namespace LocalJSX {
}
interface IonToolbarAttributes {
"color": Color;
"titlePlacement": 'start' | 'center' | 'end';
}

interface IntrinsicElements {
Expand Down
Copy link
Member Author

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.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
133 changes: 133 additions & 0 deletions core/src/components/toolbar/test/title-placement/index.html
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>
45 changes: 45 additions & 0 deletions core/src/components/toolbar/test/title-placement/toolbar.e2e.ts
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}`));
});
});
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 15 additions & 4 deletions core/src/components/toolbar/toolbar.ionic.scss
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@
@include globals.padding-horizontal(globals.$ion-space-200);
}

:host(.toolbar-title-default) ::slotted(ion-title) {
text-align: center;
}

:host(.toolbar-title-large) ::slotted(ion-title) {
@include globals.padding-horizontal(globals.$ion-space-400);
}
Expand Down Expand Up @@ -116,3 +112,18 @@

flex: 0 0 var(--primary-secondary-size, 0);
}

// Toolbar Title Placement
// --------------------------------------------------

:host(.toolbar-title-placement-start) ::slotted(ion-title) {
text-align: start;
}

:host(.toolbar-title-placement-center) ::slotted(ion-title) {
text-align: center;
}

:host(.toolbar-title-placement-end) ::slotted(ion-title) {
text-align: end;
}
53 changes: 52 additions & 1 deletion core/src/components/toolbar/toolbar.tsx
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';
Expand Down Expand Up @@ -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;

Expand All @@ -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';
Copy link
Member Author

Choose a reason for hiding this comment

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

I chose to add titlePlacement to the toolbar because it controls all the positioning logic, but if we would prefer to add placement as a property on ion-title we could assign it there and pass it up to ion-toolbar.

Copy link
Member

Choose a reason for hiding this comment

The 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 ionic theme? Setting titlePlacement="start" on an ios toolbar won't do anything visually, but there's nothing in the docs to explain why. The presentingElement and collapse props both use "Only applies in iOS mode." for the same kind of restriction.


componentWillLoad() {
const buttons = Array.from(this.el.querySelectorAll('ion-buttons'));

Expand Down Expand Up @@ -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';
Copy link
Member Author

Choose a reason for hiding this comment

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

While this property doesn't work with ios or md yet, this will be needed eventually when we update them to work with it.

}

/**
* Updates the CSS custom properties for slot widths
* This ensures that slots shown by their met conditions
Expand Down Expand Up @@ -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'];

Expand Down Expand Up @@ -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,
}}
Expand Down
4 changes: 2 additions & 2 deletions packages/angular/src/directives/proxies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2636,14 +2636,14 @@ This event will not emit when programmatically setting the `checked` property.


@ProxyCmp({
inputs: ['color', 'mode', 'theme']
inputs: ['color', 'mode', 'theme', 'titlePlacement']
})
@Component({
selector: 'ion-toolbar',
changeDetection: ChangeDetectionStrategy.OnPush,
template: '<ng-content></ng-content>',
// eslint-disable-next-line @angular-eslint/no-inputs-metadata-property
inputs: ['color', 'mode', 'theme'],
inputs: ['color', 'mode', 'theme', 'titlePlacement'],
})
export class IonToolbar {
protected el: HTMLIonToolbarElement;
Expand Down
4 changes: 2 additions & 2 deletions packages/angular/standalone/src/directives/proxies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2298,14 +2298,14 @@ Shorthand for ionToastDidDismiss.

@ProxyCmp({
defineCustomElementFn: defineIonToolbar,
inputs: ['color', 'mode', 'theme']
inputs: ['color', 'mode', 'theme', 'titlePlacement']
})
@Component({
selector: 'ion-toolbar',
changeDetection: ChangeDetectionStrategy.OnPush,
template: '<ng-content></ng-content>',
// eslint-disable-next-line @angular-eslint/no-inputs-metadata-property
inputs: ['color', 'mode', 'theme'],
inputs: ['color', 'mode', 'theme', 'titlePlacement'],
standalone: true
})
export class IonToolbar {
Expand Down
3 changes: 2 additions & 1 deletion packages/vue/src/proxies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,7 @@ export const IonToggle: StencilVueComponent<JSX.IonToggle, JSX.IonToggle["checke


export const IonToolbar: StencilVueComponent<JSX.IonToolbar> = /*@__PURE__*/ defineContainer<JSX.IonToolbar>('ion-toolbar', defineIonToolbar, [
'color'
'color',
'titlePlacement'
]);

Loading