mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-18 19:21:34 +08:00
feat(back-button): adds defaultHref
This commit is contained in:
2
packages/core/src/components.d.ts
vendored
2
packages/core/src/components.d.ts
vendored
@ -310,6 +310,8 @@ declare global {
|
|||||||
}
|
}
|
||||||
namespace JSXElements {
|
namespace JSXElements {
|
||||||
export interface IonBackButtonAttributes extends HTMLAttributes {
|
export interface IonBackButtonAttributes extends HTMLAttributes {
|
||||||
|
defaultHref?: string;
|
||||||
|
icon?: string;
|
||||||
mode?: 'ios' | 'md';
|
mode?: 'ios' | 'md';
|
||||||
text?: string|undefined;
|
text?: string|undefined;
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
.back-button-ios .back-button-inner {
|
.back-button-ios .back-button-inner {
|
||||||
@include padding(0);
|
@include padding(0);
|
||||||
@include margin(0);
|
@include margin(2px, 0, 0, 0);
|
||||||
|
|
||||||
z-index: $back-button-ios-button-z-index;
|
z-index: $back-button-ios-button-z-index;
|
||||||
overflow: visible;
|
overflow: visible;
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
// MD Back Button
|
// MD Back Button
|
||||||
// --------------------------------------------------
|
// --------------------------------------------------
|
||||||
.back-button-md .back-button-inner {
|
.back-button-md .back-button-inner {
|
||||||
@include margin(0, 6px, 0, 0);
|
@include margin(2px, 6px, 0, 0);
|
||||||
@include padding(0, 5px);
|
@include padding(0, 5px);
|
||||||
|
|
||||||
min-width: 44px;
|
min-width: 44px;
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.back-button.can-back-back,
|
||||||
.back-button.show-back-button {
|
.back-button.show-back-button {
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
}
|
}
|
||||||
@ -40,6 +41,14 @@
|
|||||||
|
|
||||||
font-smoothing: antialiased;
|
font-smoothing: antialiased;
|
||||||
-webkit-font-smoothing: antialiased;
|
-webkit-font-smoothing: antialiased;
|
||||||
|
|
||||||
|
display: flex;
|
||||||
|
|
||||||
|
flex-flow: row nowrap;
|
||||||
|
flex-shrink: 0;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.back-button .button-inner {
|
.back-button .button-inner {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { Component, Element, Prop } from '@stencil/core';
|
import { Component, Element, Prop } from '@stencil/core';
|
||||||
import { Config } from '../../index';
|
import { Config } from '../../index';
|
||||||
|
import { openURL } from '../../utils/theme';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
tag: 'ion-back-button',
|
tag: 'ion-back-button',
|
||||||
@ -13,8 +14,6 @@ import { Config } from '../../index';
|
|||||||
})
|
})
|
||||||
export class BackButton {
|
export class BackButton {
|
||||||
|
|
||||||
private custom = true;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The mode determines which platform styles to use.
|
* The mode determines which platform styles to use.
|
||||||
* Possible values are: `"ios"` or `"md"`.
|
* Possible values are: `"ios"` or `"md"`.
|
||||||
@ -28,37 +27,45 @@ export class BackButton {
|
|||||||
*/
|
*/
|
||||||
@Prop() text: string|undefined;
|
@Prop() text: string|undefined;
|
||||||
|
|
||||||
|
@Prop() icon: string;
|
||||||
|
|
||||||
|
@Prop() defaultHref: string;
|
||||||
|
|
||||||
@Prop({ context: 'config' }) config: Config;
|
@Prop({ context: 'config' }) config: Config;
|
||||||
|
|
||||||
@Element() el: HTMLElement;
|
@Element() el: HTMLElement;
|
||||||
|
|
||||||
componentWillLoad() {
|
|
||||||
this.custom = this.el.childElementCount > 0;
|
hostData() {
|
||||||
|
return {
|
||||||
|
class: {
|
||||||
|
'show-back-button': !!this.defaultHref
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private onClick(ev: Event) {
|
||||||
|
const nav = this.el.closest('ion-nav');
|
||||||
|
if (nav && nav.canGoBack()) {
|
||||||
|
ev.preventDefault();
|
||||||
|
nav.pop();
|
||||||
|
} else if (this.defaultHref) {
|
||||||
|
openURL(this.defaultHref, ev);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const backButtonIcon = this.config.get('backButtonIcon', 'arrow-back');
|
const backButtonIcon = this.icon || this.config.get('backButtonIcon', 'arrow-back');
|
||||||
const defaultBackButtonText = this.config.get('backButtonText', this.mode === 'ios' ? 'Back' : '');
|
const backButtonText = this.text || this.config.get('backButtonText', this.mode === 'ios' ? 'Back' : '');
|
||||||
const backButtonText = this.text || defaultBackButtonText;
|
|
||||||
|
|
||||||
if (this.custom) {
|
|
||||||
return (
|
return (
|
||||||
<ion-nav-pop>
|
<button
|
||||||
<slot />
|
class='back-button-inner'
|
||||||
</ion-nav-pop>
|
onClick={(ev) => this.onClick(ev)}>
|
||||||
);
|
{ backButtonIcon && <ion-icon name={backButtonIcon}/> }
|
||||||
} else {
|
{ backButtonText && <span class='button-text'>{backButtonText}</span> }
|
||||||
return (
|
|
||||||
<ion-nav-pop>
|
|
||||||
<button class='back-button-inner'>
|
|
||||||
<span class='button-inner'>
|
|
||||||
<ion-icon name={backButtonIcon} slot='start' />
|
|
||||||
<span class='button-text'>{backButtonText}</span>
|
|
||||||
</span>
|
|
||||||
{ this.mode === 'md' && <ion-ripple-effect/> }
|
{ this.mode === 'md' && <ion-ripple-effect/> }
|
||||||
</button>
|
</button>
|
||||||
</ion-nav-pop>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -15,18 +15,14 @@ add a back button to your view, all you need is:
|
|||||||
|
|
||||||
The back button component is smart enough to know what to render and what content to show.
|
The back button component is smart enough to know what to render and what content to show.
|
||||||
|
|
||||||
If, however, you want more control over what is shown in the back button, you can pass your own button markup.
|
If, however, you want more control over what is shown in the back button, you use the
|
||||||
|
`text` and `icon` properties.
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<ion-header>
|
<ion-header>
|
||||||
<ion-toolbar>
|
<ion-toolbar>
|
||||||
<ion-buttons slot="start">
|
<ion-buttons slot="start">
|
||||||
<ion-back-button>
|
<ion-back-button text="Volver" icon="add"></ion-back-button>
|
||||||
<ion-button>
|
|
||||||
Button Text
|
|
||||||
<ion-icon name="add" slot="start"></ion-icon>
|
|
||||||
</ion-button>
|
|
||||||
</ion-back-button>
|
|
||||||
</ion-buttons>
|
</ion-buttons>
|
||||||
</ion-toolbar>
|
</ion-toolbar>
|
||||||
</ion-header>
|
</ion-header>
|
||||||
@ -39,11 +35,7 @@ Or no button text at all:
|
|||||||
<ion-header>
|
<ion-header>
|
||||||
<ion-toolbar>
|
<ion-toolbar>
|
||||||
<ion-buttons slot="start">
|
<ion-buttons slot="start">
|
||||||
<ion-back-button>
|
<ion-back-button text="" icon="add"></ion-back-button>
|
||||||
<ion-button>
|
|
||||||
<ion-icon name="add" slot="icon-only"></ion-icon>
|
|
||||||
</ion-button>
|
|
||||||
</ion-back-button>
|
|
||||||
</ion-buttons>
|
</ion-buttons>
|
||||||
</ion-toolbar>
|
</ion-toolbar>
|
||||||
</ion-header>
|
</ion-header>
|
||||||
@ -55,6 +47,16 @@ Or no button text at all:
|
|||||||
|
|
||||||
## Properties
|
## Properties
|
||||||
|
|
||||||
|
#### defaultHref
|
||||||
|
|
||||||
|
string
|
||||||
|
|
||||||
|
|
||||||
|
#### icon
|
||||||
|
|
||||||
|
string
|
||||||
|
|
||||||
|
|
||||||
#### mode
|
#### mode
|
||||||
|
|
||||||
|
|
||||||
@ -74,6 +76,16 @@ default look-and-feel
|
|||||||
|
|
||||||
## Attributes
|
## Attributes
|
||||||
|
|
||||||
|
#### default-href
|
||||||
|
|
||||||
|
string
|
||||||
|
|
||||||
|
|
||||||
|
#### icon
|
||||||
|
|
||||||
|
string
|
||||||
|
|
||||||
|
|
||||||
#### mode
|
#### mode
|
||||||
|
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function goToPageTwo(nav) {
|
async function goToPageTwo(nav) {
|
||||||
const firstPage = document.createElement('div');
|
const secondPage = document.createElement('div');
|
||||||
secondPage.classList.add('second-page');
|
secondPage.classList.add('second-page');
|
||||||
secondPage.innerHTML = `
|
secondPage.innerHTML = `
|
||||||
<ion-header>
|
<ion-header>
|
||||||
@ -78,12 +78,7 @@
|
|||||||
<ion-header>
|
<ion-header>
|
||||||
<ion-toolbar>
|
<ion-toolbar>
|
||||||
<ion-buttons slot="start">
|
<ion-buttons slot="start">
|
||||||
<ion-back-button>
|
<ion-back-button color="danger" text="Text!" icon="add"></ion-back-button>
|
||||||
<ion-button color="danger">
|
|
||||||
<ion-icon name="add" slot="start"></ion-icon>
|
|
||||||
Text!
|
|
||||||
</ion-button>
|
|
||||||
</ion-back-button>
|
|
||||||
</ion-buttons>
|
</ion-buttons>
|
||||||
<ion-title>Page Three</ion-title>
|
<ion-title>Page Three</ion-title>
|
||||||
</ion-toolbar>
|
</ion-toolbar>
|
||||||
|
@ -5,11 +5,11 @@ import { Component, Element, Listen } from '@stencil/core';
|
|||||||
})
|
})
|
||||||
export class NavPop {
|
export class NavPop {
|
||||||
|
|
||||||
@Element() element: HTMLElement;
|
@Element() el: HTMLElement;
|
||||||
|
|
||||||
@Listen('child:click')
|
@Listen('child:click')
|
||||||
pop(): Promise<any> {
|
pop(): Promise<any> {
|
||||||
const nav = this.element.closest('ion-nav') as HTMLIonNavElement;
|
const nav = this.el.closest('ion-nav');
|
||||||
if (nav) {
|
if (nav) {
|
||||||
return nav.pop();
|
return nav.pop();
|
||||||
}
|
}
|
||||||
|
@ -6,14 +6,14 @@ import { Component, Element, Listen, Prop } from '@stencil/core';
|
|||||||
})
|
})
|
||||||
export class NavPush {
|
export class NavPush {
|
||||||
|
|
||||||
@Element() element: HTMLElement;
|
@Element() el: HTMLElement;
|
||||||
@Prop() component: any;
|
@Prop() component: any;
|
||||||
@Prop() url: string;
|
@Prop() url: string;
|
||||||
@Prop() data: any;
|
@Prop() data: any;
|
||||||
|
|
||||||
@Listen('child:click')
|
@Listen('child:click')
|
||||||
push(): Promise<any> {
|
push(): Promise<any> {
|
||||||
const nav = this.element.closest('ion-nav') as HTMLIonNavElement;
|
const nav = this.el.closest('ion-nav');
|
||||||
if (nav) {
|
if (nav) {
|
||||||
const toPush = this.url || this.component;
|
const toPush = this.url || this.component;
|
||||||
return nav.push(toPush, this.data);
|
return nav.push(toPush, this.data);
|
||||||
|
@ -5,14 +5,14 @@ import { Component, Element, Listen, Prop } from '@stencil/core';
|
|||||||
})
|
})
|
||||||
export class NavSetRoot {
|
export class NavSetRoot {
|
||||||
|
|
||||||
@Element() element: HTMLElement;
|
@Element() el: HTMLElement;
|
||||||
@Prop() component: any;
|
@Prop() component: any;
|
||||||
@Prop() url: string;
|
@Prop() url: string;
|
||||||
@Prop() data: any;
|
@Prop() data: any;
|
||||||
|
|
||||||
@Listen('child:click')
|
@Listen('child:click')
|
||||||
push(): Promise<any> {
|
push(): Promise<any> {
|
||||||
const nav = this.element.closest('ion-nav');
|
const nav = this.el.closest('ion-nav');
|
||||||
if (nav) {
|
if (nav) {
|
||||||
const toPush = this.url || this.component;
|
const toPush = this.url || this.component;
|
||||||
return nav.setRoot(toPush, this.data);
|
return nav.setRoot(toPush, this.data);
|
||||||
|
@ -8,7 +8,7 @@ const TRANSFORM = 'transform';
|
|||||||
const TRANSLATEX = 'translateX';
|
const TRANSLATEX = 'translateX';
|
||||||
const CENTER = '0%';
|
const CENTER = '0%';
|
||||||
const OFF_OPACITY = 0.8;
|
const OFF_OPACITY = 0.8;
|
||||||
const SHOW_BACK_BTN_CSS = 'show-back-button';
|
const SHOW_BACK_BTN_CSS = 'can-back-back';
|
||||||
|
|
||||||
export default function iosTransitionAnimation(Animation: Animation, _: HTMLElement, opts: AnimationOptions): Promise<Animation> {
|
export default function iosTransitionAnimation(Animation: Animation, _: HTMLElement, opts: AnimationOptions): Promise<Animation> {
|
||||||
|
|
||||||
@ -76,7 +76,7 @@ export default function iosTransitionAnimation(Animation: Animation, _: HTMLElem
|
|||||||
enteringToolBarBg.addElement(enteringToolBarEle.querySelector('.toolbar-background'));
|
enteringToolBarBg.addElement(enteringToolBarEle.querySelector('.toolbar-background'));
|
||||||
|
|
||||||
const enteringBackButton = new Animation();
|
const enteringBackButton = new Animation();
|
||||||
enteringBackButton.addElement(enteringToolBarEle.querySelector('.back-button'));
|
enteringBackButton.addElement(enteringToolBarEle.querySelector('ion-back-button'));
|
||||||
|
|
||||||
enteringToolBar
|
enteringToolBar
|
||||||
.add(enteringTitle)
|
.add(enteringTitle)
|
||||||
|
@ -4,7 +4,7 @@ import { isDef } from '../../../utils/helpers';
|
|||||||
const TRANSLATEY = 'translateY';
|
const TRANSLATEY = 'translateY';
|
||||||
const OFF_BOTTOM = '40px';
|
const OFF_BOTTOM = '40px';
|
||||||
const CENTER = '0px';
|
const CENTER = '0px';
|
||||||
const SHOW_BACK_BTN_CSS = 'show-back-button';
|
const SHOW_BACK_BTN_CSS = 'can-back-back';
|
||||||
|
|
||||||
export default function mdTransitionAnimation(Animation: Animation, _: HTMLElement, opts: AnimationOptions): Promise<Animation> {
|
export default function mdTransitionAnimation(Animation: Animation, _: HTMLElement, opts: AnimationOptions): Promise<Animation> {
|
||||||
|
|
||||||
@ -39,7 +39,7 @@ export default function mdTransitionAnimation(Animation: Animation, _: HTMLEleme
|
|||||||
rootTransition.add(enteringToolBar);
|
rootTransition.add(enteringToolBar);
|
||||||
|
|
||||||
const enteringBackButton = new Animation();
|
const enteringBackButton = new Animation();
|
||||||
enteringBackButton.addElement(enteringToolbarEle.querySelector('.back-button'));
|
enteringBackButton.addElement(enteringToolbarEle.querySelector('ion-back-button'));
|
||||||
rootTransition.add(enteringBackButton);
|
rootTransition.add(enteringBackButton);
|
||||||
if (opts.enteringView.enableBack()) {
|
if (opts.enteringView.enableBack()) {
|
||||||
enteringBackButton.beforeAddClass(SHOW_BACK_BTN_CSS);
|
enteringBackButton.beforeAddClass(SHOW_BACK_BTN_CSS);
|
||||||
|
Reference in New Issue
Block a user