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

@ -631,34 +631,36 @@ export const SegmentButtonExample: React.FC = () => (
## CSS Custom Properties
| Name | Description |
| ------------------------ | ----------------------------------------------------------------------------------------------------------------- |
| `--background` | Background of the segment button |
| `--background-checked` | Background of the checked segment button |
| `--background-disabled` | Background of the disabled segment button |
| `--background-hover` | Background of the segment button on hover |
| `--border-color` | Color of the segment button border |
| `--border-radius` | Radius of the segment button border |
| `--border-style` | Style of the segment button border |
| `--border-width` | Width of the segment button border |
| `--color` | Color of the segment button |
| `--color-checked` | Color of the checked segment button |
| `--color-disabled` | Color of the disabled segment button |
| `--color-hover` | Color of the segment button on hover |
| `--indicator-box-shadow` | Box shadow on the indicator for the checked segment button |
| `--indicator-color` | Color of the indicator for the checked segment button |
| `--indicator-height` | Height of the indicator for the checked segment button |
| `--indicator-transform` | Transform of the indicator for the checked segment button |
| `--indicator-transition` | Transition of the indicator for the checked segment button |
| `--margin-bottom` | Bottom margin of the segment button |
| `--margin-end` | Right margin if direction is left-to-right, and left margin if direction is right-to-left of the segment button |
| `--margin-start` | Left margin if direction is left-to-right, and right margin if direction is right-to-left of the segment button |
| `--margin-top` | Top margin of the segment button |
| `--padding-bottom` | Bottom padding of the segment button |
| `--padding-end` | Right padding if direction is left-to-right, and left padding if direction is right-to-left of the segment button |
| `--padding-start` | Left padding if direction is left-to-right, and right padding if direction is right-to-left of the segment button |
| `--padding-top` | Top padding of the segment button |
| `--transition` | Transition of the segment button |
| Name | Description |
| ------------------------------ | ----------------------------------------------------------------------------------------------------------------- |
| `--background` | Background of the segment button |
| `--background-checked` | Background of the checked segment button |
| `--background-focused` | Background of the segment button when focused with the tab key |
| `--background-focused-opacity` | Opacity of the segment button background when focused with the tab key |
| `--background-hover` | Background of the segment button on hover |
| `--background-hover-opacity` | Opacity of the segment button background on hover |
| `--border-color` | Color of the segment button border |
| `--border-radius` | Radius of the segment button border |
| `--border-style` | Style of the segment button border |
| `--border-width` | Width of the segment button border |
| `--color` | Color of the segment button |
| `--color-checked` | Color of the checked segment button |
| `--color-focused` | Color of the segment button when focused with the tab key |
| `--color-hover` | Color of the segment button on hover |
| `--indicator-box-shadow` | Box shadow on the indicator for the checked segment button |
| `--indicator-color` | Color of the indicator for the checked segment button |
| `--indicator-height` | Height of the indicator for the checked segment button |
| `--indicator-transform` | Transform of the indicator for the checked segment button |
| `--indicator-transition` | Transition of the indicator for the checked segment button |
| `--margin-bottom` | Bottom margin of the segment button |
| `--margin-end` | Right margin if direction is left-to-right, and left margin if direction is right-to-left of the segment button |
| `--margin-start` | Left margin if direction is left-to-right, and right margin if direction is right-to-left of the segment button |
| `--margin-top` | Top margin of the segment button |
| `--padding-bottom` | Bottom padding of the segment button |
| `--padding-end` | Right padding if direction is left-to-right, and left padding if direction is right-to-left of the segment button |
| `--padding-start` | Left padding if direction is left-to-right, and right padding if direction is right-to-left of the segment button |
| `--padding-top` | Top padding of the segment button |
| `--transition` | Transition of the segment button |
## Dependencies

View File

@ -5,9 +5,12 @@
// --------------------------------------------------
:host {
--background: #{$segment-button-ios-background-color};
--background-checked: #{$segment-button-ios-background-color-checked};
--background-hover: #{$segment-button-ios-background-color-hover};
--background: #{$segment-button-ios-background};
--background-checked: #{$segment-button-ios-background-checked};
--background-hover: #{$segment-button-ios-background-hover};
--background-hover-opacity: 0;
--background-focused: none;
--background-focused-opacity: 0;
--border-radius: #{$segment-button-ios-border-radius};
--border-width: #{$segment-button-ios-border-width};
--border-color: #{$segment-button-ios-border-color};
@ -134,16 +137,89 @@
}
// Segment: States
// --------------------------------------------------
:host(.ion-focused) .button-native {
opacity: $segment-button-ios-opacity-focused;
}
@media (any-hover: hover) {
// Default Segment, Hover
:host(:hover) .button-native {
opacity: $segment-button-ios-opacity-hover;
}
:host(.segment-button-checked:hover) .button-native {
opacity: 1;
}
}
// Segment Button: Segment w/ Color
// --------------------------------------------------
// Default
:host(.in-segment-color) {
background: none;
color: $segment-button-ios-color;
}
// Indicator color on a Segment w/ color
// should not change if the variable is set
:host(.in-segment-color) .segment-button-indicator-background {
background: $segment-button-ios-indicator-color;
}
@media (any-hover: hover) {
// Toolbar with Color, Default Segment, Hover
// Toolbar with Color, Default Segment, Checked, Hover
:host(.in-segment-color:hover) .button-native,
:host(.in-segment-color.segment-button-checked:hover) .button-native {
color: $segment-button-ios-color;
}
}
// Segment Button: Toolbar
// --------------------------------------------------
// Segment button indicator color should use the global variable with
// a fallback to the local variable
:host(.in-toolbar) .segment-button-indicator-background {
background: #{var(--ion-toolbar-segment-indicator-color, var(--indicator-color))};
// Default Segment, In a Toolbar
:host(.in-toolbar:not(.in-segment-color)) {
--background-checked: #{var(--ion-toolbar-segment-background-checked, $segment-button-ios-background-checked)};
--color: var(--ion-toolbar-segment-color, var(--ion-toolbar-color), initial);
--color-checked: var(--ion-toolbar-segment-color-checked, var(--ion-toolbar-color), initial);
--indicator-color: #{var(--ion-toolbar-segment-indicator-color, $segment-button-ios-indicator-color)};
}
// Segment Button: Toolbar w/ Color
// --------------------------------------------------
// Do not use the global or local CSS variable if the toolbar has a color
:host(.in-toolbar-color) .segment-button-indicator-background {
background: #fff;
}
// Toolbar with Color, Default Segment
:host(.in-toolbar-color:not(.in-segment-color)) .button-native {
color: #{current-color(contrast)};
}
// Toolbar with Color, Default Segment, Checked
:host(.in-toolbar-color.segment-button-checked:not(.in-segment-color)) .button-native {
color: #{current-color(base)};
}
@media (any-hover: hover) {
// Toolbar with Color, Default Segment, Hover
:host(.in-toolbar-color:not(.in-segment-color):hover) .button-native {
color: #{current-color(contrast)};
}
// Toolbar with Color, Default Segment, Checked / Hover
:host(.in-toolbar-color.segment-button-checked:not(.in-segment-color):hover) .button-native {
color: #{current-color(base)};
}
}

View File

@ -4,10 +4,13 @@
// --------------------------------------------------
/// @prop - Background of the segment button
$segment-button-ios-background-color: transparent !default;
$segment-button-ios-background: none !default;
/// @prop - Background of the checked segment button
$segment-button-ios-background-color-checked: transparent !default;
$segment-button-ios-background-checked: $segment-button-ios-background !default;
/// @prop - Text color of the segment button
$segment-button-ios-color: $text-color !default;
/// @prop - Background of the checked segment button indicator
$segment-button-ios-indicator-color: var(--ion-color-step-350, $background-color) !default;
@ -18,11 +21,14 @@ $segment-button-ios-margin: 2px !default;
/// @prop - Opacity of the segment button on hover
$segment-button-ios-opacity-hover: .5 !default;
/// @prop - Opacity of the segment button when focused
$segment-button-ios-opacity-focused: .7 !default;
/// @prop - Opacity of the disabled segment button
$segment-button-ios-opacity-disabled: .3 !default;
/// @prop - Background of the segment button on hover
$segment-button-ios-background-color-hover: transparent !default;
$segment-button-ios-background-hover: none !default;
/// @prop - Box shadow of the checked segment button
$segment-button-ios-box-shadow-checked: 0 0 5px rgba(0, 0, 0, 0.16) !default;

View File

@ -7,7 +7,11 @@
:host {
--background: #{$segment-button-md-background};
--background-checked: #{$segment-button-md-background-checked};
--background-hover: #{$segment-button-md-background-hover};
--background-hover: var(--color-checked);
--background-focused: var(--color-checked);
--background-activated-opacity: 0;
--background-focused-opacity: .12;
--background-hover-opacity: .04;
--color: #{$segment-button-md-text-color};
--color-checked: #{$segment-button-md-text-color-checked};
--indicator-box-shadow: none;
@ -48,6 +52,84 @@
opacity: $segment-button-md-opacity-disabled;
}
// Segment Button: Segment w/ Color
// --------------------------------------------------
// Default
:host(.in-segment-color) {
background: none;
color: $segment-button-md-text-color;
}
// Indicator color and ripple on a Segment w/ color
// should not change if the variables are set
:host(.in-segment-color) ion-ripple-effect {
color: #{current-color(base)};
}
:host(.in-segment-color) .segment-button-indicator-background {
background: #{current-color(base)};
}
// Checked
:host(.in-segment-color.segment-button-checked) .button-native {
color: #{current-color(base)};
}
// Focused
:host(.in-segment-color.ion-focused) .button-native::after {
background: #{current-color(base)};
}
@media (any-hover: hover) {
:host(.in-segment-color:hover) .button-native {
color: $segment-button-md-text-color;
&::after {
background: #{current-color(base)};
}
}
:host(.in-segment-color.segment-button-checked:hover) .button-native {
color: #{current-color(base)};
}
}
// Segment: Default Toolbar
// --------------------------------------------------
:host(.in-toolbar:not(.in-segment-color)) {
--background: #{var(--ion-toolbar-segment-background, $segment-button-md-background)};
--background-checked: #{var(--ion-toolbar-segment-background-checked, $segment-button-md-background-checked)};
--color: #{var(--ion-toolbar-segment-color, $segment-button-md-text-color)};
--color-checked: #{var(--ion-toolbar-segment-color-checked, $segment-button-md-text-color-checked)};
--indicator-color: #{var(--ion-toolbar-segment-color-checked, var(--color-checked))};
}
// Segment Button: Toolbar w/ Color
// --------------------------------------------------
// Default Segment, In a Toolbar with Color
:host(.in-toolbar-color:not(.in-segment-color)) .button-native {
color: #{current-color(contrast, .6)};
}
// Default Segment, In a Toolbar with Color, Checked
:host(.in-toolbar-color.segment-button-checked:not(.in-segment-color)) .button-native {
color: #{current-color(contrast)};
}
// Default Segment, In a Toolbar with Color, Hover
@media (any-hover: hover) {
:host(.in-toolbar-color:not(.in-segment-color)) .button-native::after {
background: #{current-color(contrast)};
}
}
// Segment Button: Icon
// --------------------------------------------------

View File

@ -9,24 +9,21 @@ $segment-button-md-opacity: .6 !default;
/// @prop - Text color of the segment button
$segment-button-md-text-color: rgba($text-color-rgb, $segment-button-md-opacity) !default;
/// @prop - Text color of the checked segment button
$segment-button-md-text-color-checked: ion-color(primary, base) !default;
/// @prop - Background of the segment button
$segment-button-md-background: none !default;
/// @prop - Background of the checked segment button
$segment-button-md-background-checked: $segment-button-md-background !default;
/// @prop - Background of the hovered segment button
$segment-button-md-background-hover: ion-color(primary, base, .04) !default;
/// @prop - Width of the bottom border on the segment button
$segment-button-md-border-bottom-width: 2px !default;
/// @prop - Color of the bottom border on the segment button
$segment-button-md-border-bottom-color: transparent !default;
/// @prop - Text color of the checked segment button
$segment-button-md-text-color-checked: ion-color(primary, base) !default;
/// @prop - Opacity of the disabled segment button
$segment-button-md-opacity-disabled: .3 !default;

View File

@ -7,13 +7,17 @@
/**
* @prop --background: Background of the segment button
* @prop --background-checked: Background of the checked segment button
* @prop --background-disabled: Background of the disabled segment button
*
* @prop --background-hover: Background of the segment button on hover
* @prop --background-focused: Background of the segment button when focused with the tab key
*
* @prop --background-hover-opacity: Opacity of the segment button background on hover
* @prop --background-focused-opacity: Opacity of the segment button background when focused with the tab key
*
* @prop --color: Color of the segment button
* @prop --color-checked: Color of the checked segment button
* @prop --color-disabled: Color of the disabled segment button
* @prop --color-hover: Color of the segment button on hover
* @prop --color-focused: Color of the segment button when focused with the tab key
*
* @prop --border-radius: Radius of the segment button border
* @prop --border-color: Color of the segment button border
@ -39,7 +43,7 @@
* @prop --indicator-transform: Transform of the indicator for the checked segment button
*/
--color: initial;
--color-hover: initial;
--color-hover: var(--color);
--color-checked: var(--color);
--color-disabled: var(--color);
--padding-start: 0;
@ -105,9 +109,29 @@
contain: content;
cursor: pointer;
overflow: hidden;
z-index: 2;
}
.button-native::after {
@include button-state();
}
.button-inner {
display: flex;
position: relative;
flex-flow: inherit;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
z-index: 1;
}
// Segment Button: Checked
// --------------------------------------------------
@ -122,22 +146,40 @@
// --------------------------------------------------
:host(.segment-button-disabled) {
background: var(--background-disabled);
color: var(--color-disabled);
cursor: default;
pointer-events: none;
}
// Segment Button: Focused
// --------------------------------------------------
:host(.ion-focused) .button-native {
color: var(--color-focused);
&::after {
background: var(--background-focused);
opacity: var(--background-focused-opacity);
}
}
// Segment Button: Hover
// --------------------------------------------------
@media (any-hover: hover) {
:host(:hover) {
background: var(--background-hover);
color: var(--color-hover, var(--color));
:host(:hover) .button-native {
color: var(--color-hover);
&::after {
background: var(--background-hover);
opacity: var(--background-hover-opacity);
}
}
:host(.segment-button-checked:hover) {
color: var(--color-hover, var(--color-checked));
:host(.segment-button-checked:hover) .button-native {
color: var(--color-checked);
}
}

View File

@ -88,6 +88,8 @@ export class SegmentButton implements ComponentInterface, ButtonInterface {
[mode]: true,
'in-toolbar': hostContext('ion-toolbar', this.el),
'in-toolbar-color': hostContext('ion-toolbar[color]', this.el),
'in-segment': hostContext('ion-segment', this.el),
'in-segment-color': hostContext('ion-segment[color]', this.el),
'segment-button-has-label': hasLabel,
'segment-button-has-icon': hasIcon,
'segment-button-has-label-only': hasLabel && !hasIcon,
@ -97,6 +99,7 @@ export class SegmentButton implements ComponentInterface, ButtonInterface {
[`segment-button-layout-${layout}`]: true,
'ion-activatable': true,
'ion-activatable-instant': true,
'ion-focusable': true,
}}
>
<button
@ -105,7 +108,9 @@ export class SegmentButton implements ComponentInterface, ButtonInterface {
class="button-native"
disabled={disabled}
>
<slot></slot>
<span class="button-inner">
<slot></slot>
</span>
{mode === 'md' && <ion-ripple-effect></ion-ripple-effect>}
</button>
<div