mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-20 12:29:55 +08:00

Before users had to know the exact opacity that the MD/iOS spec called for in order to change the hover or focused background color. This allows them to change the background without having to know the opacity. - changes apply to Action Sheet (Buttons), Back Button, Button, FAB Button, Item, Menu Button, Segment Button, Tab Button - greatly reduces the requirement by users to set the background hover, focused states for dark modes and custom themes, also eliminates the need to know what the hover opacity is for each based on the spec - updates the MD dark theme per their spec - adds a component guide for internal use changing Ionic components references #18279 fixes #20213 fixes #19965 BREAKING CHANGE: *Activated Class* The `activated` class that is automatically added to buttons on press has been renamed to `ion-activated`. This will be more consistent with our `ion-focused` class we add and also will reduce conflicts with user's CSS. *CSS Variables* The `--background-hover`, `--background-focused` and `--background-activated` CSS variables on components that render native buttons will now have an opacity automatically set. If you are setting any of these like the following: ``` --background-hover: rgba(44, 44, 44, 0.08); ``` You will likely not see a hover state anymore. It should be updated to only set the desired color: ``` --background-hover: rgba(44, 44, 44); ``` If the opacity desired is something other than what the spec asks for, use: ``` --background-hover: rgba(44, 44, 44); --background-hover-opacity: 1; ```
174 lines
4.7 KiB
TypeScript
174 lines
4.7 KiB
TypeScript
import { Component, ComponentInterface, Element, Event, EventEmitter, Host, Listen, Prop, h } from '@stencil/core';
|
|
|
|
import { config } from '../../global/config';
|
|
import { getIonMode } from '../../global/ionic-global';
|
|
import { TabBarChangedEventDetail, TabButtonClickEventDetail, TabButtonLayout } from '../../interface';
|
|
import { AnchorInterface } from '../../utils/element-interface';
|
|
|
|
/**
|
|
* @virtualProp {"ios" | "md"} mode - The mode determines which platform styles to use.
|
|
*/
|
|
@Component({
|
|
tag: 'ion-tab-button',
|
|
styleUrls: {
|
|
ios: 'tab-button.ios.scss',
|
|
md: 'tab-button.md.scss'
|
|
},
|
|
shadow: true
|
|
})
|
|
export class TabButton implements ComponentInterface, AnchorInterface {
|
|
|
|
@Element() el!: HTMLElement;
|
|
|
|
/**
|
|
* If `true`, the user cannot interact with the tab button.
|
|
*/
|
|
@Prop() disabled = false;
|
|
|
|
/**
|
|
* This attribute instructs browsers to download a URL instead of navigating to
|
|
* it, so the user will be prompted to save it as a local file. If the attribute
|
|
* has a value, it is used as the pre-filled file name in the Save prompt
|
|
* (the user can still change the file name if they want).
|
|
*/
|
|
@Prop() download: string | undefined;
|
|
|
|
/**
|
|
* Contains a URL or a URL fragment that the hyperlink points to.
|
|
* If this property is set, an anchor tag will be rendered.
|
|
*/
|
|
@Prop() href: string | undefined;
|
|
|
|
/**
|
|
* Specifies the relationship of the target object to the link object.
|
|
* The value is a space-separated list of [link types](https://developer.mozilla.org/en-US/docs/Web/HTML/Link_types).
|
|
*/
|
|
@Prop() rel: string | undefined;
|
|
|
|
/**
|
|
* Set the layout of the text and icon in the tab bar.
|
|
* It defaults to `'icon-top'`.
|
|
*/
|
|
@Prop({ mutable: true }) layout?: TabButtonLayout;
|
|
|
|
/**
|
|
* The selected tab component
|
|
*/
|
|
@Prop({ mutable: true }) selected = false;
|
|
|
|
/**
|
|
* A tab id must be provided for each `ion-tab`. It's used internally to reference
|
|
* the selected tab or by the router to switch between them.
|
|
*/
|
|
@Prop() tab?: string;
|
|
|
|
/**
|
|
* Specifies where to display the linked URL.
|
|
* Only applies when an `href` is provided.
|
|
* Special keywords: `"_blank"`, `"_self"`, `"_parent"`, `"_top"`.
|
|
*/
|
|
@Prop() target: string | undefined;
|
|
|
|
/**
|
|
* Emitted when the tab bar is clicked
|
|
* @internal
|
|
*/
|
|
@Event() ionTabButtonClick!: EventEmitter<TabButtonClickEventDetail>;
|
|
|
|
@Listen('ionTabBarChanged', { target: 'parent' })
|
|
onTabBarChanged(ev: CustomEvent<TabBarChangedEventDetail>) {
|
|
this.selected = this.tab === ev.detail.tab;
|
|
}
|
|
|
|
componentWillLoad() {
|
|
if (this.layout === undefined) {
|
|
this.layout = config.get('tabButtonLayout', 'icon-top');
|
|
}
|
|
}
|
|
|
|
private selectTab(ev: Event | KeyboardEvent) {
|
|
if (this.tab !== undefined) {
|
|
if (!this.disabled) {
|
|
this.ionTabButtonClick.emit({
|
|
tab: this.tab,
|
|
href: this.href,
|
|
selected: this.selected
|
|
});
|
|
}
|
|
ev.preventDefault();
|
|
}
|
|
}
|
|
|
|
private get hasLabel() {
|
|
return !!this.el.querySelector('ion-label');
|
|
}
|
|
|
|
private get hasIcon() {
|
|
return !!this.el.querySelector('ion-icon');
|
|
}
|
|
|
|
private get tabIndex() {
|
|
if (this.disabled) { return -1; }
|
|
|
|
const hasTabIndex = this.el.hasAttribute('tabindex');
|
|
|
|
if (hasTabIndex) {
|
|
return this.el.getAttribute('tabindex');
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
private onKeyUp = (ev: KeyboardEvent) => {
|
|
if (ev.key === 'Enter' || ev.key === ' ') {
|
|
this.selectTab(ev);
|
|
}
|
|
}
|
|
|
|
private onClick = (ev: Event) => {
|
|
this.selectTab(ev);
|
|
}
|
|
|
|
render() {
|
|
const { disabled, hasIcon, hasLabel, tabIndex, href, rel, target, layout, selected, tab } = this;
|
|
const mode = getIonMode(this);
|
|
const attrs = {
|
|
download: this.download,
|
|
href,
|
|
rel,
|
|
target
|
|
};
|
|
|
|
return (
|
|
<Host
|
|
onClick={this.onClick}
|
|
onKeyup={this.onKeyUp}
|
|
role="tab"
|
|
tabindex={tabIndex}
|
|
aria-selected={selected ? 'true' : null}
|
|
id={tab !== undefined ? `tab-button-${tab}` : null}
|
|
class={{
|
|
[mode]: true,
|
|
'tab-selected': selected,
|
|
'tab-disabled': disabled,
|
|
'tab-has-label': hasLabel,
|
|
'tab-has-icon': hasIcon,
|
|
'tab-has-label-only': hasLabel && !hasIcon,
|
|
'tab-has-icon-only': hasIcon && !hasLabel,
|
|
[`tab-layout-${layout}`]: true,
|
|
'ion-activatable': true,
|
|
'ion-selectable': true,
|
|
'ion-focusable': true
|
|
}}
|
|
>
|
|
<a {...attrs} tabIndex={-1} class="button-native">
|
|
<span class="button-inner">
|
|
<slot></slot>
|
|
</span>
|
|
{mode === 'md' && <ion-ripple-effect type="unbounded"></ion-ripple-effect>}
|
|
</a>
|
|
</Host>
|
|
);
|
|
}
|
|
}
|