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:
Brandy Carney
2020-01-23 16:57:47 -05:00
committed by GitHub
parent 445f129e2d
commit 94159291b2
114 changed files with 3641 additions and 1487 deletions

View File

@ -5,7 +5,10 @@
// --------------------------------------------------
:host {
--background-focused: #{ion-color(primary, base, .1)};
--background-hover: transparent;
--background-hover-opacity: 1;
--background-focused: currentColor;
--background-focused-opacity: .1;
--border-radius: 4px;
--color: #{$back-button-ios-color};
--icon-margin-end: -5px;
@ -26,21 +29,20 @@
// Back Button States
// --------------------------------------------------
:host(.activated) .button-native {
:host(.ion-activated) .button-native {
opacity: .4;
}
@media (any-hover: hover) {
:host(:hover) {
--opacity: .6;
opacity: .6;
}
}
// Back Button Color: Focused
// --------------------------------------------------
:host(.ion-color.ion-focused) .button-native {
background: #{current-color(base, .1)};
:host(.ion-color.ion-focused) .button-native::after {
background: #{current-color(base)};
}

View File

@ -6,8 +6,10 @@
:host {
--border-radius: 4px;
--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: 0.04;
--color: #{$back-button-md-color};
--icon-margin-end: 0;
--icon-margin-start: 0;
@ -50,8 +52,8 @@ ion-icon {
// --------------------------------------------------
@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)};
}
}
@ -59,6 +61,6 @@ ion-icon {
// Back Button Color: Focused
// --------------------------------------------------
:host(.ion-color.ion-focused) .button-native {
background: #{current-color(base, .24)};
:host(.ion-color.ion-focused) .button-native::after {
background: #{current-color(base)};
}

View File

@ -7,11 +7,13 @@
/**
* @prop --background: Background of the button
* @prop --background-focused: Background of the button when focused with the tab key
* @prop --background-hover: Background of the button when hover
* @prop --background-focused-opacity: Opacity of the button background when focused with the tab key
* @prop --background-hover: Background of the button on hover
* @prop --background-hover-opacity: Opacity of the background on hover
*
* @prop --color: Text color of the button
* @prop --color-focused: Text color of the button when focused with the tab key
* @prop --color-hover: Text color of the button when hover
* @prop --color-hover: Text color of the button on hover
*
* @prop --min-width: Minimum width of the button
* @prop --min-height: Minimum height of the button
@ -48,8 +50,8 @@
* @prop --icon-font-weight: Font weight of the button icon
*/
--background: transparent;
--color-focused: var(--color);
--color-hover: var(--color);
--color-focused: currentColor;
--color-hover: currentColor;
--icon-margin-top: 0;
--icon-margin-bottom: 0;
--icon-padding-top: 0;
@ -143,6 +145,8 @@
cursor: pointer;
opacity: var(--opacity);
overflow: hidden;
user-select: none;
z-index: 0;
appearance: none;
@ -150,6 +154,7 @@
.button-inner {
display: flex;
position: relative;
flex-flow: row nowrap;
flex-shrink: 0;
@ -158,6 +163,8 @@
width: 100%;
height: 100%;
z-index: 1;
}
@ -177,22 +184,45 @@ ion-icon {
}
// Back Button: Hover
// --------------------------------------------------
@media (any-hover: hover) {
:host(:hover) .button-native {
background: var(--background-hover);
color: var(--color-hover);
}
}
// Back 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);
}
}
// Back 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);
}
}
}
// Back Button Color: Focused
// --------------------------------------------------
:host(.ion-color.ion-focused) .button-native {
color: #{current-color(base)};
}
@ -206,17 +236,9 @@ ion-icon {
}
// Back Button Color: Focused
// --------------------------------------------------
:host(.ion-color.ion-focused) .button-native {
color: #{current-color(base)};
}
// Back Button in Toolbar: Global Theming
// --------------------------------------------------
:host-context(ion-toolbar:not(.ion-color)):not(.ion-color) {
:host(.in-toolbar) {
color: #{var(--ion-toolbar-color, var(--color))};
}

View File

@ -4,7 +4,7 @@ import { config } from '../../global/config';
import { getIonMode } from '../../global/ionic-global';
import { Color } from '../../interface';
import { ButtonInterface } from '../../utils/element-interface';
import { createColorClasses, openURL } from '../../utils/theme';
import { createColorClasses, hostContext, openURL } from '../../utils/theme';
/**
* @virtualProp {"ios" | "md"} mode - The mode determines which platform styles to use.
@ -113,6 +113,7 @@ export class BackButton implements ComponentInterface, ButtonInterface {
'button': true, // ion-buttons target .button
'back-button-disabled': disabled,
'back-button-has-icon-only': hasIconOnly,
'in-toolbar': hostContext('ion-toolbar', this.el),
'ion-activatable': true,
'ion-focusable': true,
'show-back-button': showBackButton

View File

@ -245,38 +245,40 @@ export const BackButtonExample: React.FC = () => (
## CSS Custom Properties
| Name | Description |
| ----------------------- | -------------------------------------------------------------------------------------------------------------- |
| `--background` | Background of the button |
| `--background-focused` | Background of the button when focused with the tab key |
| `--background-hover` | Background of the button when hover |
| `--border-radius` | Border radius of the button |
| `--color` | Text color of the button |
| `--color-focused` | Text color of the button when focused with the tab key |
| `--color-hover` | Text color of the button when hover |
| `--icon-font-size` | Font size of the button icon |
| `--icon-font-weight` | Font weight of the button icon |
| `--icon-margin-bottom` | Bottom margin of the button icon |
| `--icon-margin-end` | Right margin if direction is left-to-right, and left margin if direction is right-to-left of the button icon |
| `--icon-margin-start` | Left margin if direction is left-to-right, and right margin if direction is right-to-left of the button icon |
| `--icon-margin-top` | Top margin of the button icon |
| `--icon-padding-bottom` | Bottom padding of the button icon |
| `--icon-padding-end` | Right padding if direction is left-to-right, and left padding if direction is right-to-left of the button icon |
| `--icon-padding-start` | Left padding if direction is left-to-right, and right padding if direction is right-to-left of the button icon |
| `--icon-padding-top` | Top padding of the button icon |
| `--margin-bottom` | Bottom margin of the button |
| `--margin-end` | Right margin if direction is left-to-right, and left margin if direction is right-to-left of the button |
| `--margin-start` | Left margin if direction is left-to-right, and right margin if direction is right-to-left of the button |
| `--margin-top` | Top margin of the button |
| `--min-height` | Minimum height of the button |
| `--min-width` | Minimum width of the button |
| `--opacity` | Opacity of the button |
| `--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 |
| `--ripple-color` | Color of the button ripple effect |
| `--transition` | Transition of the button |
| Name | Description |
| ------------------------------ | -------------------------------------------------------------------------------------------------------------- |
| `--background` | Background of the button |
| `--background-focused` | Background of the button when focused with the tab key |
| `--background-focused-opacity` | Opacity of the button background when focused with the tab key |
| `--background-hover` | Background of the button on hover |
| `--background-hover-opacity` | Opacity of the background on hover |
| `--border-radius` | Border radius of the button |
| `--color` | Text color of the button |
| `--color-focused` | Text color of the button when focused with the tab key |
| `--color-hover` | Text color of the button on hover |
| `--icon-font-size` | Font size of the button icon |
| `--icon-font-weight` | Font weight of the button icon |
| `--icon-margin-bottom` | Bottom margin of the button icon |
| `--icon-margin-end` | Right margin if direction is left-to-right, and left margin if direction is right-to-left of the button icon |
| `--icon-margin-start` | Left margin if direction is left-to-right, and right margin if direction is right-to-left of the button icon |
| `--icon-margin-top` | Top margin of the button icon |
| `--icon-padding-bottom` | Bottom padding of the button icon |
| `--icon-padding-end` | Right padding if direction is left-to-right, and left padding if direction is right-to-left of the button icon |
| `--icon-padding-start` | Left padding if direction is left-to-right, and right padding if direction is right-to-left of the button icon |
| `--icon-padding-top` | Top padding of the button icon |
| `--margin-bottom` | Bottom margin of the button |
| `--margin-end` | Right margin if direction is left-to-right, and left margin if direction is right-to-left of the button |
| `--margin-start` | Left margin if direction is left-to-right, and right margin if direction is right-to-left of the button |
| `--margin-top` | Top margin of the button |
| `--min-height` | Minimum height of the button |
| `--min-width` | Minimum width of the button |
| `--opacity` | Opacity of the button |
| `--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 |
| `--ripple-color` | Color of the button ripple effect |
| `--transition` | Transition of the button |
## Dependencies

View File

@ -10,7 +10,8 @@
<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>
<script type="module" src="../../../../../dist/ionic/ionic.esm.js"></script>
</head>
<body>
<ion-app>
@ -75,19 +76,63 @@
<p>
<ion-back-button class="custom"></ion-back-button>
<ion-back-button color="secondary" class="custom"></ion-back-button>
</p>
<p>
<ion-back-button class="custom ion-focused"></ion-back-button>
<ion-back-button color="secondary" class="custom ion-focused"></ion-back-button>
</p>
<ion-toolbar>
<ion-buttons slot="start">
<ion-back-button></ion-back-button>
<ion-back-button class="ion-focused"></ion-back-button>
</ion-buttons>
<ion-title>Default</ion-title>
</ion-toolbar>
<ion-toolbar color="primary">
<ion-buttons slot="start">
<ion-back-button></ion-back-button>
<ion-back-button class="ion-focused"></ion-back-button>
</ion-buttons>
<ion-title>Primary</ion-title>
</ion-toolbar>
<ion-toolbar color="light">
<ion-buttons slot="start">
<ion-back-button></ion-back-button>
<ion-back-button class="ion-focused"></ion-back-button>
</ion-buttons>
<ion-title>Light</ion-title>
</ion-toolbar>
<ion-toolbar color="success">
<ion-buttons slot="start">
<ion-back-button></ion-back-button>
<ion-back-button class="ion-focused"></ion-back-button>
</ion-buttons>
<ion-title>Success</ion-title>
</ion-toolbar>
<ion-toolbar class="themed">
<ion-buttons slot="start">
<ion-back-button></ion-back-button>
<ion-back-button class="ion-focused"></ion-back-button>
</ion-buttons>
<ion-title>Themed</ion-title>
</ion-toolbar>
<ion-toolbar color="dark">
<ion-buttons slot="start">
<ion-back-button class="ion-hide"></ion-back-button>
</ion-buttons>
<ion-title>Hidden</ion-title>
</ion-toolbar>
</ion-content>
</ion-app>
<script>
var buttons = document.querySelectorAll('ion-back-button');
for(var i = 0; i < buttons.length; i++) {
for (var i = 0; i < buttons.length; i++) {
buttons[i].addEventListener('click', (event) => onClick(event));
}
@ -113,7 +158,9 @@
.wide {
--background: #d1f3ff;
--background-hover: #add8e6;
--background-hover-opacity: 1;
--background-focused: #84c5db;
--background-focused-opacity: 1;
height: 50px;
width: 150px;
@ -140,6 +187,14 @@
--padding-end: 10px;
}
.custom:hover {
opacity: 1;
}
.themed {
--ion-toolbar-background: #222;
--ion-toolbar-color: #ddd;
}
</style>
</body>

View File

@ -34,15 +34,15 @@
<ion-back-button color="dark"></ion-back-button>
</p>
<p>
<ion-back-button color="primary" class="activated"></ion-back-button>
<ion-back-button color="secondary" class="activated"></ion-back-button>
<ion-back-button color="tertiary" class="activated"></ion-back-button>
<ion-back-button color="success" class="activated"></ion-back-button>
<ion-back-button color="warning" class="activated"></ion-back-button>
<ion-back-button color="danger" class="activated"></ion-back-button>
<ion-back-button color="light" class="activated"></ion-back-button>
<ion-back-button color="medium" class="activated"></ion-back-button>
<ion-back-button color="dark" class="activated"></ion-back-button>
<ion-back-button color="primary" class="ion-activated"></ion-back-button>
<ion-back-button color="secondary" class="ion-activated"></ion-back-button>
<ion-back-button color="tertiary" class="ion-activated"></ion-back-button>
<ion-back-button color="success" class="ion-activated"></ion-back-button>
<ion-back-button color="warning" class="ion-activated"></ion-back-button>
<ion-back-button color="danger" class="ion-activated"></ion-back-button>
<ion-back-button color="light" class="ion-activated"></ion-back-button>
<ion-back-button color="medium" class="ion-activated"></ion-back-button>
<ion-back-button color="dark" class="ion-activated"></ion-back-button>
</p>
<h3>Custom</h3>
@ -54,13 +54,13 @@
<!-- Custom Colors -->
<ion-back-button class="custom"></ion-back-button>
<ion-back-button class="custom activated"></ion-back-button>
<ion-back-button class="custom ion-activated"></ion-back-button>
<ion-back-button color="secondary" class="custom"></ion-back-button>
<ion-toolbar>
<ion-buttons slot="start">
<ion-back-button></ion-back-button>
<ion-back-button class="activated"></ion-back-button>
<ion-back-button class="ion-activated"></ion-back-button>
</ion-buttons>
<ion-title>Default</ion-title>
</ion-toolbar>
@ -68,7 +68,7 @@
<ion-toolbar>
<ion-buttons slot="start">
<ion-back-button class="custom"></ion-back-button>
<ion-back-button class="custom activated"></ion-back-button>
<ion-back-button class="custom ion-activated"></ion-back-button>
</ion-buttons>
<ion-title>Custom</ion-title>
</ion-toolbar>
@ -76,7 +76,7 @@
<ion-toolbar>
<ion-buttons slot="start">
<ion-back-button color="secondary"></ion-back-button>
<ion-back-button color="secondary" class="activated"></ion-back-button>
<ion-back-button color="secondary" class="ion-activated"></ion-back-button>
</ion-buttons>
<ion-title>Secondary</ion-title>
</ion-toolbar>
@ -84,7 +84,7 @@
<ion-toolbar color="danger">
<ion-buttons slot="start">
<ion-back-button></ion-back-button>
<ion-back-button class="activated"></ion-back-button>
<ion-back-button class="ion-activated"></ion-back-button>
</ion-buttons>
<ion-title>Danger</ion-title>
</ion-toolbar>
@ -92,7 +92,7 @@
<ion-toolbar color="dark">
<ion-buttons slot="start">
<ion-back-button text="Back" icon=""></ion-back-button>
<ion-back-button text="Back" icon="" class="activated"></ion-back-button>
<ion-back-button text="Back" icon="" class="ion-activated"></ion-back-button>
</ion-buttons>
<ion-title>Dark</ion-title>
</ion-toolbar>