Skip to content

Commit

Permalink
Toggle: Refining style variables approach in Toggle and adding exampl…
Browse files Browse the repository at this point in the history
…es passing interface and callback function (microsoft#7177)

* Removing some of the deprecated components flaged when turning the tslint deprecation flag to true in the office-ui-fabric-react package.

* Adding corresponding change file.

* Reverting changes that affected backwards compatibility because it removed own deprecated usage.

* Adding missing deprecation reversion in BaseButton.

* Adding changes in oufr api.

* Refining style variables approach in Toggle and adding examples passing interface and function.

* Adding change file.

* Updating change file.

* Getting rid of if-else use in favor of inline checks.
  • Loading branch information
khmakoto committed Nov 27, 2018
1 parent 6ec50ae commit 57a7af2
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "@uifabric/experiments",
"comment": "Toggle: Refining style variables approach in Toggle and adding examples passing interface and function.",
"type": "minor"
}
],
"packageName": "@uifabric/experiments",
"email": "Humberto.Morimoto@microsoft.com"
}
117 changes: 63 additions & 54 deletions packages/experiments/src/components/Toggle/Toggle.styles.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { IToggleComponent, IToggleStyles, IToggleStyleVariablesTypes, IToggleStyleVariables, IToggleStates } from './Toggle.types';
import { IToggleComponent, IToggleStyles, IToggleStyleVariablesTypes, IToggleStyleVariables, IToggleViewProps } from './Toggle.types';
import { getFocusStyle, getGlobalClassNames, HighContrastSelector, concatStyleSets } from '../../Styling';
import { processVariables } from '../../utilities/VariableProcessing';

const GlobalClassNames = {
root: 'ms-Toggle',
Expand All @@ -17,60 +16,52 @@ export const ToggleStyles: IToggleComponent['styles'] = props => {

const classNames = getGlobalClassNames(GlobalClassNames, theme);

const toggleStyleVariables: IToggleStyleVariables = {
baseState: {},
const toggleBaseStateVariables: IToggleStyleVariablesTypes = {};

enabled: {
pillBackground: semanticColors.bodyBackground,
pillBorderColor: semanticColors.smallInputBorder,
pillHoveredBorderColor: semanticColors.inputBorderHovered,
pillHighContrastBorderColor: 'Highlight',
pillHighContrastHoveredBorderColor: 'Highlight',
const toggleEnabledVariables: IToggleStyleVariablesTypes = {
pillBackground: semanticColors.bodyBackground,
pillBorderColor: semanticColors.smallInputBorder,
pillHoveredBorderColor: semanticColors.inputBorderHovered,
pillHighContrastBorderColor: 'Highlight',
pillHighContrastHoveredBorderColor: 'Highlight',

thumbBackground: semanticColors.inputBorderHovered
},

disabled: {
pillBackground: semanticColors.bodyBackground,
pillBorderColor: semanticColors.disabledBodyText,

thumbBackground: semanticColors.disabledBodyText,
thumbBackground: semanticColors.inputBorderHovered
};

textColor: semanticColors.disabledText,
textHighContrastColor: 'GrayText'
},
const toggleDisabledVariables: IToggleStyleVariablesTypes = {
pillBackground: semanticColors.bodyBackground,
pillBorderColor: semanticColors.disabledBodyText,

checked: {
pillBackground: semanticColors.inputBackgroundChecked,
pillHoveredBackground: semanticColors.inputBackgroundCheckedHovered,
pillBorderColor: 'transparent',
pillHoveredBorderColor: 'transparent',
pillJustifyContent: 'flex-end',
pillHighContrastBackground: 'WindowText',
pillHighContrastHoveredBackground: 'Highlight',
pillHighContrastBorderColor: 'Highlight',
thumbBackground: semanticColors.disabledBodyText,

thumbBackground: semanticColors.inputForegroundChecked,
thumbHighContrastBackground: 'Window',
thumbHighContrastBorderColor: 'Window'
},
textColor: semanticColors.disabledText,
textHighContrastColor: 'GrayText'
};

checkedDisabled: {
pillBackground: semanticColors.disabledBodyText,
pillBorderColor: 'transparent',
pillJustifyContent: 'flex-end',
const toggleCheckedVariables: IToggleStyleVariablesTypes = {
pillBorderColor: 'transparent',
pillJustifyContent: 'flex-end'
};

thumbBackground: semanticColors.disabledBackground,
const toggleCheckedEnabledVariables: IToggleStyleVariablesTypes = {
pillBackground: semanticColors.inputBackgroundChecked,
pillHoveredBackground: semanticColors.inputBackgroundCheckedHovered,
pillHoveredBorderColor: 'transparent',
pillHighContrastBackground: 'WindowText',
pillHighContrastHoveredBackground: 'Highlight',
pillHighContrastHoveredBorderColor: 'transparent',

thumbBackground: semanticColors.inputForegroundChecked,
thumbHighContrastBackground: 'Window',
thumbHighContrastBorderColor: 'Window'
};

textColor: semanticColors.disabledText,
textHighContrastColor: 'GrayText'
},
const toggleCheckedDisabledVariables: IToggleStyleVariablesTypes = {
pillBackground: semanticColors.disabledBodyText,

...styleVariables
thumbBackground: semanticColors.disabledBackground
};

const toggleVariables = processVariables(toggleStyleVariables);

function getToggleStylesFromState(state: IToggleStyleVariablesTypes): Partial<IToggleStyles> {
if (state) {
return {
Expand Down Expand Up @@ -208,15 +199,33 @@ export const ToggleStyles: IToggleComponent['styles'] = props => {
return {};
}

function getToggleStylesFromVariant(variantVariables: { [PState in IToggleStates]: IToggleStyleVariablesTypes }): Partial<IToggleStyles> {
return concatStyleSets(
getToggleStylesFromState(variantVariables.baseState),
!disabled && !checked && getToggleStylesFromState(variantVariables.enabled),
disabled && !checked && getToggleStylesFromState(variantVariables.disabled),
!disabled && checked && getToggleStylesFromState(variantVariables.checked),
disabled && checked && getToggleStylesFromState(variantVariables.checkedDisabled)
);
function getToggleViewStyleVariables(viewProps: IToggleViewProps): IToggleStyleVariablesTypes {
let toggleVariables: IToggleStyleVariablesTypes = toggleBaseStateVariables;

toggleVariables = {
...toggleVariables,
...(viewProps.checked ? toggleCheckedVariables : {}),
...(viewProps.disabled
? { ...toggleDisabledVariables, ...(viewProps.checked ? toggleCheckedDisabledVariables : {}) }
: { ...toggleEnabledVariables, ...(viewProps.checked ? toggleCheckedEnabledVariables : {}) })
};

return toggleVariables;
}

function getToggleStyles(userStyleVariables: IToggleStyleVariables): Partial<IToggleStyles> {
let toggleVariables: IToggleStyleVariablesTypes;

toggleVariables = getToggleViewStyleVariables(props);

if (typeof userStyleVariables === 'function') {
toggleVariables = { ...toggleVariables, ...userStyleVariables(props) };
} else if (userStyleVariables !== undefined) {
toggleVariables = { ...toggleVariables, ...userStyleVariables };
}

return getToggleStylesFromState(toggleVariables);
}

return concatStyleSets(getToggleStylesFromVariant(toggleVariables), { root: className });
return concatStyleSets(getToggleStyles(styleVariables), { root: className });
};
4 changes: 1 addition & 3 deletions packages/experiments/src/components/Toggle/Toggle.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { IRawStyleBase } from '@uifabric/merge-styles/lib/IRawStyleBase';

export type IToggleComponent = IComponent<IToggleProps, IToggleViewProps, IToggleStyles>;

export type IToggleStates = 'baseState' | 'enabled' | 'disabled' | 'checked' | 'checkedDisabled';

export type IToggleSlots = 'root' | 'label' | 'container' | 'pill' | 'thumb' | 'text';

export interface IToggle {
Expand Down Expand Up @@ -129,6 +127,6 @@ export interface IToggleStyleVariablesTypes {
textHighContrastColor?: string;
}

export type IToggleStyleVariables = { [PState in IToggleStates]?: IToggleStyleVariablesTypes };
export type IToggleStyleVariables = IToggleStyleVariablesTypes | ((props: IToggleViewProps) => IToggleStyleVariablesTypes) | undefined;

export type IToggleStyles = { [key in IToggleSlots]: IStyle };
14 changes: 13 additions & 1 deletion packages/experiments/src/components/Toggle/TogglePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { ExampleCard, IComponentDemoPageProps, ComponentPage, PageMarkdown, Prop
import { ToggleExample } from './examples/Toggle.Example';
const ToggleExampleCode = require('!raw-loader!@uifabric/experiments/src/components/Toggle/examples/Toggle.Example.tsx') as string;

import { ToggleStyleVarsInterfaceExample } from './examples/Toggle.StyleVarsInterface.Example';
const ToggleStyleVarsInterfaceExampleCode = require('!raw-loader!@uifabric/experiments/src/components/Toggle/examples/Toggle.StyleVarsInterface.Example.tsx') as string;

import { ToggleStyleVarsFunctionExample } from './examples/Toggle.StyleVarsFunction.Example';
const ToggleStyleVarsFunctionExampleCode = require('!raw-loader!@uifabric/experiments/src/components/Toggle/examples/Toggle.StyleVarsFunction.Example.tsx') as string;

export class TogglePage extends React.Component<IComponentDemoPageProps, {}> {
public render(): JSX.Element {
return (
Expand All @@ -12,9 +18,15 @@ export class TogglePage extends React.Component<IComponentDemoPageProps, {}> {
componentName=" Toggle"
exampleCards={
<div>
<ExampleCard title=" Default Toggles" code={ToggleExampleCode}>
<ExampleCard title="Default Toggles" code={ToggleExampleCode}>
<ToggleExample />
</ExampleCard>
<ExampleCard title="Toggle components when modifying style variables" code={ToggleStyleVarsInterfaceExampleCode}>
<ToggleStyleVarsInterfaceExample />
</ExampleCard>
<ExampleCard title="Toggle components when sending a function as style variables" code={ToggleStyleVarsFunctionExampleCode}>
<ToggleStyleVarsFunctionExample />
</ExampleCard>
</div>
}
propertiesTables={
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import * as React from 'react';
import { Toggle } from '../index';
import { IToggleViewProps, IToggleStyleVariablesTypes } from '../Toggle.types';

const styleVarsFunction = (props: IToggleViewProps): IToggleStyleVariablesTypes => {
return {
...(props.checked ? { textColor: 'green' } : { textColor: 'red' }),
...(props.disabled
? { ...{ pillBackground: 'gainsboro' }, ...(props.checked ? { pillBackground: 'slategrey' } : {}) }
: { ...{ pillBackground: 'turquoise' }, ...(props.checked ? { pillBackground: 'navy' } : {}) })
};
};

export class ToggleStyleVarsFunctionExample extends React.Component<{}, {}> {
public render(): JSX.Element {
return (
<div>
<Toggle
defaultChecked={true}
label="Enabled and checked"
onText="On"
offText="Off"
onChange={this._onChange}
styleVariables={styleVarsFunction}
/>
<Toggle
defaultChecked={false}
label="Enabled and unchecked"
onText="On"
offText="Off"
onChange={this._onChange}
styleVariables={styleVarsFunction}
/>
<Toggle
defaultChecked={true}
disabled={true}
label="Disabled and checked"
onText="On"
offText="Off"
styleVariables={styleVarsFunction}
/>
<Toggle
defaultChecked={false}
disabled={true}
label="Disabled and unchecked"
onText="On"
offText="Off"
styleVariables={styleVarsFunction}
/>
</div>
);
}

private _onChange(ev: React.MouseEvent<HTMLElement>, checked: boolean) {
console.log('toggle is ' + (checked ? 'checked' : 'not checked'));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as React from 'react';
import { Toggle } from '../index';
import { IToggleStyleVariablesTypes } from '../Toggle.types';

const styleVars1: IToggleStyleVariablesTypes = {
pillHoveredBackground: 'black',
textColor: 'red'
};

const styleVars2: IToggleStyleVariablesTypes = {
pillBackground: 'orange',
pillJustifyContent: 'center',
textColor: 'purple',
thumbBackground: 'green'
};

const styleVars3: IToggleStyleVariablesTypes = {
pillBackground: 'dimgrey',
pillBorderColor: 'brown',
textColor: 'darkred'
};

export class ToggleStyleVarsInterfaceExample extends React.Component<{}, {}> {
public render(): JSX.Element {
return (
<div>
<Toggle defaultChecked={true} label="Example 1" onText="On" offText="Off" onChange={this._onChange} styleVariables={styleVars1} />
<Toggle defaultChecked={false} label="Example 2" onText="On" offText="Off" onChange={this._onChange} styleVariables={styleVars2} />
<Toggle defaultChecked={true} disabled={true} label="Example 3" onText="On - Disabled" offText="Off" styleVariables={styleVars3} />
</div>
);
}

private _onChange(ev: React.MouseEvent<HTMLElement>, checked: boolean) {
console.log('toggle is ' + (checked ? 'checked' : 'not checked'));
}
}

0 comments on commit 57a7af2

Please sign in to comment.