-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Expand file tree
/
Copy pathback-button.tsx
More file actions
186 lines (162 loc) · 5.66 KB
/
back-button.tsx
File metadata and controls
186 lines (162 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import caretLeftRegular from '@phosphor-icons/core/assets/regular/caret-left.svg';
import type { ComponentInterface } from '@stencil/core';
import { Component, Element, Host, Prop, h } from '@stencil/core';
import type { ButtonInterface } from '@utils/element-interface';
import type { Attributes } from '@utils/helpers';
import { inheritAriaAttributes, openURL } from '@utils/helpers';
import { createColorClasses, hostContext } from '@utils/theme';
import { arrowBackSharp, chevronBack } from 'ionicons/icons';
import { config } from '../../global/config';
import { getIonTheme } from '../../global/ionic-global';
import type { AnimationBuilder, Color } from '../../interface';
/**
* @virtualProp {"ios" | "md"} mode - The mode determines the platform behaviors of the component.
* @virtualProp {"ios" | "md" | "ionic"} theme - The theme determines the visual appearance of the component.
*
* @part native - The native HTML button element that wraps all child elements.
* @part icon - The back button icon (uses ion-icon).
* @part text - The back button text.
*/
@Component({
tag: 'ion-back-button',
styleUrls: {
ios: 'back-button.ios.scss',
md: 'back-button.md.scss',
ionic: 'back-button.md.scss',
},
shadow: true,
})
export class BackButton implements ComponentInterface, ButtonInterface {
private inheritedAttributes: Attributes = {};
@Element() el!: HTMLElement;
/**
* The color to use from your application's color palette.
* Default options are: `"primary"`, `"secondary"`, `"tertiary"`, `"success"`, `"warning"`, `"danger"`, `"light"`, `"medium"`, and `"dark"`.
* For more information on colors, see [theming](/docs/theming/basics).
*/
@Prop({ reflect: true }) color?: Color;
/**
* The url to navigate back to by default when there is no history.
*/
@Prop({ mutable: true }) defaultHref?: string;
/**
* If `true`, the user cannot interact with the button.
*/
@Prop({ reflect: true }) disabled = false;
/**
* The built-in named SVG icon name or the exact `src` of an SVG file
* to use for the back button.
*/
@Prop() icon?: string | null;
/**
* The text to display in the back button.
*/
@Prop() text?: string | null;
/**
* The type of the button.
*/
@Prop() type: 'submit' | 'reset' | 'button' = 'button';
/**
* When using a router, it specifies the transition animation when navigating to
* another page.
*/
@Prop() routerAnimation: AnimationBuilder | undefined;
componentWillLoad() {
this.inheritedAttributes = inheritAriaAttributes(this.el);
if (this.defaultHref === undefined) {
this.defaultHref = config.get('backButtonDefaultHref');
}
}
get backButtonIcon() {
// Return the icon if it is explicitly set
if (this.icon != null) {
return this.icon;
}
// Determine the theme and map to default icons
const theme = getIonTheme(this);
const defaultIcons = {
ios: chevronBack,
ionic: caretLeftRegular,
md: arrowBackSharp,
};
// Get the default icon based on the theme, falling back to 'md' icon if necessary
const defaultIcon = defaultIcons[theme] || defaultIcons.md;
// Return the configured back button icon or the default icon
return config.get('backButtonIcon', defaultIcon);
}
get backButtonText() {
const defaultBackButtonText = getIonTheme(this) === 'ios' ? 'Back' : null;
return this.text != null ? this.text : config.get('backButtonText', defaultBackButtonText);
}
get hasIconOnly() {
return this.backButtonIcon && !this.backButtonText;
}
get rippleType() {
// If the button only has an icon we use the unbounded
// "circular" ripple effect
if (this.hasIconOnly) {
return 'unbounded';
}
return 'bounded';
}
private onClick = async (ev: Event) => {
const nav = this.el.closest('ion-nav');
ev.preventDefault();
if (nav && (await nav.canGoBack())) {
return nav.pop({ animationBuilder: this.routerAnimation, skipIfBusy: true });
}
return openURL(this.defaultHref, ev, 'back', this.routerAnimation);
};
render() {
const {
color,
defaultHref,
disabled,
type,
hasIconOnly,
backButtonIcon,
backButtonText,
icon,
inheritedAttributes,
} = this;
const showBackButton = defaultHref !== undefined;
const theme = getIonTheme(this);
const ariaLabel = inheritedAttributes['aria-label'] || backButtonText || 'back';
return (
<Host
onClick={this.onClick}
class={createColorClasses(color, {
[theme]: true,
button: true, // ion-buttons target .button
'back-button-disabled': disabled,
'back-button-has-icon-only': hasIconOnly,
'in-toolbar': hostContext('ion-toolbar', this.el),
'in-toolbar-color': hostContext('ion-toolbar[color]', this.el),
'ion-activatable': true,
'ion-focusable': true,
'show-back-button': showBackButton,
})}
>
<button type={type} disabled={disabled} class="button-native" part="native" aria-label={ariaLabel}>
<span class="button-inner">
{backButtonIcon && (
<ion-icon
part="icon"
icon={backButtonIcon}
aria-hidden="true"
lazy={false}
flip-rtl={icon === undefined}
></ion-icon>
)}
{backButtonText && (
<span part="text" aria-hidden="true" class="button-text">
{backButtonText}
</span>
)}
</span>
{theme === 'md' && <ion-ripple-effect type={this.rippleType}></ion-ripple-effect>}
</button>
</Host>
);
}
}