mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-22 21:48:42 +08:00
feat(components): improve button states and add new css properties (#19440)
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; ```
This commit is contained in:
@ -1,10 +1,12 @@
|
||||
@import "../../themes/ionic.globals.ios";
|
||||
@import "./menu-button";
|
||||
|
||||
// iOS Menu Button
|
||||
// --------------------------------------------------
|
||||
|
||||
:host {
|
||||
--background-focused: #{ion-color(primary, base, .1)};
|
||||
--background-focused: currentColor;
|
||||
--background-focused-opacity: .1;
|
||||
--border-radius: 4px;
|
||||
--color: #{ion-color(primary, base)};
|
||||
--padding-start: 5px;
|
||||
@ -15,20 +17,20 @@
|
||||
font-size: 31px;
|
||||
}
|
||||
|
||||
:host(.activated) {
|
||||
|
||||
// Menu Button: Activated
|
||||
// --------------------------------------------------
|
||||
|
||||
:host(.ion-activated) {
|
||||
opacity: .4;
|
||||
}
|
||||
|
||||
|
||||
// Menu Button: Hover
|
||||
// --------------------------------------------------
|
||||
|
||||
@media (any-hover: hover) {
|
||||
:host(:hover) {
|
||||
opacity: .6;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Menu Button Color: Focused
|
||||
// --------------------------------------------------
|
||||
|
||||
:host(.ion-color.ion-focused) .button-native {
|
||||
background: #{current-color(base, .1)};
|
||||
}
|
@ -1,11 +1,14 @@
|
||||
@import "../../themes/ionic.globals.md";
|
||||
@import "./menu-button";
|
||||
|
||||
// MD Menu Button
|
||||
// --------------------------------------------------
|
||||
|
||||
:host {
|
||||
--background-focused: rgba(66, 66, 66, 0.24);
|
||||
--background-hover: rgba(66, 66, 66, 0.08);
|
||||
--background-focused: currentColor;
|
||||
--background-focused-opacity: .12;
|
||||
--background-hover: currentColor;
|
||||
--background-hover-opacity: .04;
|
||||
--border-radius: 50%;
|
||||
--color: initial;
|
||||
--padding-start: 8px;
|
||||
@ -18,20 +21,19 @@
|
||||
}
|
||||
|
||||
|
||||
// Menu Button Color: Focused
|
||||
// --------------------------------------------------
|
||||
|
||||
:host(.ion-color.ion-focused)::after {
|
||||
background: #{current-color(base)};
|
||||
}
|
||||
|
||||
|
||||
// Menu Button Color: Hover
|
||||
// --------------------------------------------------
|
||||
|
||||
@media (any-hover: hover) {
|
||||
:host(.ion-color:hover) .button-native {
|
||||
background: #{current-color(base, .08)};
|
||||
:host(.ion-color:hover) .button-native::after {
|
||||
background: #{current-color(base)};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Menu Button Color: Focused
|
||||
// --------------------------------------------------
|
||||
|
||||
:host(.ion-color.ion-focused) .button-native {
|
||||
background: #{current-color(base, .24)};
|
||||
color: #{current-color(base)};
|
||||
}
|
@ -9,7 +9,9 @@
|
||||
*
|
||||
* @prop --background: Background of the menu button
|
||||
* @prop --background-hover: Background of the menu button on hover
|
||||
* @prop --background-hover-opacity: Opacity of the background on hover
|
||||
* @prop --background-focused: Background of the menu button when focused with the tab key
|
||||
* @prop --background-focused-opacity: Opacity of the menu button background when focused with the tab key
|
||||
*
|
||||
* @prop --color: Color of the menu button
|
||||
* @prop --color-hover: Color of the menu button on hover
|
||||
@ -21,7 +23,7 @@
|
||||
* @prop --padding-start: Left padding if direction is left-to-right, and right padding if direction is right-to-left of the button
|
||||
*/
|
||||
--background: transparent;
|
||||
--color-focused: var(--color);
|
||||
--color-focused: currentColor;
|
||||
--border-radius: initial;
|
||||
--padding-top: 0;
|
||||
--padding-bottom: 0;
|
||||
@ -65,11 +67,28 @@
|
||||
line-height: 1;
|
||||
|
||||
cursor: pointer;
|
||||
|
||||
overflow: hidden;
|
||||
user-select: none;
|
||||
z-index: 0;
|
||||
appearance: none;
|
||||
}
|
||||
|
||||
.button-inner {
|
||||
display: flex;
|
||||
position: relative;
|
||||
|
||||
flex-flow: row nowrap;
|
||||
flex-shrink: 0;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
|
||||
z-index: 1;
|
||||
}
|
||||
|
||||
ion-icon {
|
||||
@include margin(0);
|
||||
@include padding(0);
|
||||
@ -94,23 +113,37 @@ ion-icon {
|
||||
}
|
||||
|
||||
|
||||
// Menu Button: Hover
|
||||
// --------------------------------------------------
|
||||
|
||||
@media (any-hover: hover) {
|
||||
:host(:hover) .button-native {
|
||||
background: var(--background-hover);
|
||||
color: var(--color-hover);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Menu Button: Focused
|
||||
// --------------------------------------------------
|
||||
|
||||
:host(.ion-focused) .button-native {
|
||||
background: var(--background-focused);
|
||||
color: var(--color-focused);
|
||||
|
||||
&::after {
|
||||
background: var(--background-focused);
|
||||
|
||||
opacity: var(--background-focused-opacity);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Menu Button: Hover
|
||||
// --------------------------------------------------
|
||||
|
||||
.button-native::after {
|
||||
@include button-state();
|
||||
}
|
||||
|
||||
@media (any-hover: hover) {
|
||||
:host(:hover) .button-native {
|
||||
color: var(--color-hover);
|
||||
|
||||
&::after {
|
||||
background: var(--background-hover);
|
||||
|
||||
opacity: var(--background-hover-opacity, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -125,7 +158,6 @@ ion-icon {
|
||||
// Menu Button in Toolbar: Global Theming
|
||||
// --------------------------------------------------
|
||||
|
||||
// TODO this will not work in Safari - component is shadow not scoped
|
||||
:host-context(ion-toolbar:not(.ion-color)) {
|
||||
:host(.in-toolbar) {
|
||||
color: #{var(--ion-toolbar-color, var(--color))};
|
||||
}
|
||||
|
@ -1,11 +1,11 @@
|
||||
import { Component, ComponentInterface, Host, Listen, Prop, State, h } from '@stencil/core';
|
||||
import { Component, ComponentInterface, Element, Host, Listen, Prop, State, h } from '@stencil/core';
|
||||
|
||||
import { config } from '../../global/config';
|
||||
import { getIonMode } from '../../global/ionic-global';
|
||||
import { Color } from '../../interface';
|
||||
import { ButtonInterface } from '../../utils/element-interface';
|
||||
import { menuController } from '../../utils/menu-controller';
|
||||
import { createColorClasses } from '../../utils/theme';
|
||||
import { createColorClasses, hostContext } from '../../utils/theme';
|
||||
import { updateVisibility } from '../menu-toggle/menu-toggle-util';
|
||||
|
||||
@Component({
|
||||
@ -17,6 +17,7 @@ import { updateVisibility } from '../menu-toggle/menu-toggle-util';
|
||||
shadow: true
|
||||
})
|
||||
export class MenuButton implements ComponentInterface, ButtonInterface {
|
||||
@Element() el!: HTMLIonSegmentElement;
|
||||
|
||||
@State() visible = false;
|
||||
|
||||
@ -84,6 +85,7 @@ export class MenuButton implements ComponentInterface, ButtonInterface {
|
||||
'button': true, // ion-buttons target .button
|
||||
'menu-button-hidden': hidden,
|
||||
'menu-button-disabled': disabled,
|
||||
'in-toolbar': hostContext('ion-toolbar', this.el),
|
||||
'ion-activatable': true,
|
||||
'ion-focusable': true
|
||||
}}
|
||||
@ -93,9 +95,11 @@ export class MenuButton implements ComponentInterface, ButtonInterface {
|
||||
disabled={disabled}
|
||||
class="button-native"
|
||||
>
|
||||
<slot>
|
||||
<ion-icon icon={menuIcon} mode={mode} lazy={false}></ion-icon>
|
||||
</slot>
|
||||
<span class="button-inner">
|
||||
<slot>
|
||||
<ion-icon icon={menuIcon} mode={mode} lazy={false}></ion-icon>
|
||||
</slot>
|
||||
</span>
|
||||
{mode === 'md' && <ion-ripple-effect type="unbounded"></ion-ripple-effect>}
|
||||
</button>
|
||||
</Host>
|
||||
|
@ -19,19 +19,21 @@ Menu Button is component that automatically creates the icon and functionality t
|
||||
|
||||
## CSS Custom Properties
|
||||
|
||||
| Name | Description |
|
||||
| ---------------------- | --------------------------------------------------------------------------------------------------------- |
|
||||
| `--background` | Background of the menu button |
|
||||
| `--background-focused` | Background of the menu button when focused with the tab key |
|
||||
| `--background-hover` | Background of the menu button on hover |
|
||||
| `--border-radius` | Border radius of the menu button |
|
||||
| `--color` | Color of the menu button |
|
||||
| `--color-focused` | Color of the menu button when focused with the tab key |
|
||||
| `--color-hover` | Color of the menu button on hover |
|
||||
| `--padding-bottom` | Bottom padding of the button |
|
||||
| `--padding-end` | Right padding if direction is left-to-right, and left padding if direction is right-to-left of the button |
|
||||
| `--padding-start` | Left padding if direction is left-to-right, and right padding if direction is right-to-left of the button |
|
||||
| `--padding-top` | Top padding of the button |
|
||||
| Name | Description |
|
||||
| ------------------------------ | --------------------------------------------------------------------------------------------------------- |
|
||||
| `--background` | Background of the menu button |
|
||||
| `--background-focused` | Background of the menu button when focused with the tab key |
|
||||
| `--background-focused-opacity` | Opacity of the menu button background when focused with the tab key |
|
||||
| `--background-hover` | Background of the menu button on hover |
|
||||
| `--background-hover-opacity` | Opacity of the background on hover |
|
||||
| `--border-radius` | Border radius of the menu button |
|
||||
| `--color` | Color of the menu button |
|
||||
| `--color-focused` | Color of the menu button when focused with the tab key |
|
||||
| `--color-hover` | Color of the menu button on hover |
|
||||
| `--padding-bottom` | Bottom padding of the button |
|
||||
| `--padding-end` | Right padding if direction is left-to-right, and left padding if direction is right-to-left of the button |
|
||||
| `--padding-start` | Left padding if direction is left-to-right, and right padding if direction is right-to-left of the button |
|
||||
| `--padding-top` | Top padding of the button |
|
||||
|
||||
|
||||
## Dependencies
|
||||
|
@ -85,6 +85,14 @@
|
||||
<ion-title>Success</ion-title>
|
||||
</ion-toolbar>
|
||||
|
||||
<ion-toolbar class="themed">
|
||||
<ion-buttons slot="start">
|
||||
<ion-menu-button auto-hide="false"></ion-menu-button>
|
||||
<ion-menu-button auto-hide="false" class="ion-focused"></ion-menu-button>
|
||||
</ion-buttons>
|
||||
<ion-title>Themed</ion-title>
|
||||
</ion-toolbar>
|
||||
|
||||
<ion-toolbar color="dark">
|
||||
<ion-buttons slot="start">
|
||||
<ion-menu-button></ion-menu-button>
|
||||
@ -103,13 +111,18 @@
|
||||
display: inline-block;
|
||||
}
|
||||
|
||||
.themed {
|
||||
--ion-toolbar-background: #222;
|
||||
--ion-toolbar-color: #ddd;
|
||||
}
|
||||
|
||||
.custom {
|
||||
--color: hotpink;
|
||||
}
|
||||
|
||||
.custom.md {
|
||||
--background-hover: rgb(255, 105, 180, .08);
|
||||
--background-focused: rgb(255, 105, 180, .24);
|
||||
--background-hover: #ff69b4;
|
||||
--background-focused: #ff69b4;
|
||||
}
|
||||
|
||||
.custom-large {
|
||||
|
Reference in New Issue
Block a user