Skip to content

Commit

Permalink
Merge pull request storybookjs#13711 from storybookjs/feature/remove-…
Browse files Browse the repository at this point in the history
…animation-backgrounds-a11y

Addon-backgrounds: Respect user's reduced motion settings
  • Loading branch information
shilman authored Jul 13, 2021
2 parents 783cfd3 + 6db035b commit c399d5e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
10 changes: 8 additions & 2 deletions addons/backgrounds/src/decorators/withBackground.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { StoryFn as StoryFunction, StoryContext, useMemo, useEffect } from '@storybook/addons';

import { PARAM_KEY as BACKGROUNDS_PARAM_KEY } from '../constants';
import { clearStyles, addBackgroundStyle, getBackgroundColorByName } from '../helpers';
import {
clearStyles,
addBackgroundStyle,
getBackgroundColorByName,
isReduceMotionEnabled,
} from '../helpers';

export const withBackground = (StoryFn: StoryFunction, context: StoryContext) => {
const { globals, parameters } = context;
Expand Down Expand Up @@ -29,10 +34,11 @@ export const withBackground = (StoryFn: StoryFunction, context: StoryContext) =>
context.viewMode === 'docs' ? `#anchor--${context.id} .docs-story` : '.sb-show-main';

const backgroundStyles = useMemo(() => {
const transitionStyle = 'transition: background-color 0.3s;';
return `
${selector} {
background: ${selectedBackgroundColor} !important;
transition: background-color 0.3s;
${isReduceMotionEnabled() ? '' : transitionStyle}
}
`;
}, [selectedBackgroundColor, selector]);
Expand Down
7 changes: 6 additions & 1 deletion addons/backgrounds/src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import { logger } from '@storybook/client-logger';

import { Background } from '../types';

const { document } = global;
const { document, window } = global;

export const isReduceMotionEnabled = () => {
const prefersReduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)');
return prefersReduceMotion.matches;
};

export const getBackgroundColorByName = (
currentSelectedValue: string,
Expand Down

0 comments on commit c399d5e

Please sign in to comment.