Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spinner: Change spinner icon when prefers-reduced-motion is set #87641

Merged
merged 5 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion packages/grafana-ui/src/components/Spinner/Spinner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { GrafanaTheme2 } from '@grafana/data';

import { useStyles2 } from '../../themes';
import { IconSize, isIconSize } from '../../types';
import { t } from '../../utils/i18n';
import { Icon } from '../Icon/Icon';
import { getIconRoot, getIconSubDir } from '../Icon/utils';

Expand Down Expand Up @@ -38,6 +39,8 @@ export const Spinner = ({
const styles = useStyles2(getStyles);

const deprecatedStyles = useStyles2(getDeprecatedStyles, size);
const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
const iconName = prefersReducedMotion ? 'hourglass' : 'spinner';

// this entire if statement is handling the deprecated size prop
// TODO remove once we fully remove the deprecated type
Expand Down Expand Up @@ -80,7 +83,17 @@ export const Spinner = ({
className
)}
>
<Icon className={cx('fa-spin', iconClassName)} name="spinner" size={size} aria-label="loading spinner" />
<Icon
className={cx(
{
'fa-spin': !prefersReducedMotion,
},
iconClassName
)}
name={iconName}
size={size}
aria-label={t('grafana-ui.spinner.aria-label', 'Loading')}
/>
</div>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ const renderWithProvider = ({ initialState }: { initialState?: Partial<appTypes.

describe('OrganisationSwitcher', () => {
beforeEach(() => {
(window.matchMedia as jest.Mock).mockImplementation(() => ({
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
matches: true,
}));
jest.spyOn(window, 'matchMedia').mockImplementation(
() =>
({
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
matches: true,
}) as unknown as MediaQueryList
);
});

it('should only render if more than one organisations', () => {
Expand Down Expand Up @@ -80,11 +83,14 @@ describe('OrganisationSwitcher', () => {
});

it('should render a picker in mobile screen', () => {
(window.matchMedia as jest.Mock).mockImplementation(() => ({
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
matches: false,
}));
jest.spyOn(window, 'matchMedia').mockImplementation(
() =>
({
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
matches: false,
}) as unknown as MediaQueryList
);
renderWithProvider({
initialState: {
organization: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ const renderComponent = (options?: { props: TopSearchBarSectionProps }) => {

describe('TopSearchBarSection', () => {
it('should use a wrapper on non mobile screen', () => {
(window.matchMedia as jest.Mock).mockImplementation(() => ({
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
matches: true,
}));
jest.spyOn(window, 'matchMedia').mockImplementation(
() =>
({
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
matches: true,
}) as unknown as MediaQueryList
);

const component = renderComponent();

Expand All @@ -27,11 +30,14 @@ describe('TopSearchBarSection', () => {
});

it('should not use a wrapper on mobile screen', () => {
(window.matchMedia as jest.Mock).mockImplementation(() => ({
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
matches: false,
}));
jest.spyOn(window, 'matchMedia').mockImplementation(
() =>
({
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
matches: false,
}) as unknown as MediaQueryList
);

const component = renderComponent();

Expand Down
3 changes: 3 additions & 0 deletions public/locales/en-US/grafana.json
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,9 @@
"select": {
"no-options-label": "No options found",
"placeholder": "Choose"
},
"spinner": {
"aria-label": "Loading"
}
},
"graph": {
Expand Down
3 changes: 3 additions & 0 deletions public/locales/pseudo-LOCALE/grafana.json
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,9 @@
"select": {
"no-options-label": "艃艖 艖p脓寞艖艍艧 茠艖奴艍膽",
"placeholder": "C磨艖艖艧臋"
},
"spinner": {
"aria-label": "目艖盲膽寞艍模"
}
},
"graph": {
Expand Down
22 changes: 9 additions & 13 deletions public/test/jest-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,15 @@ global.grafanaBootData = {
navTree: [],
};

// https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
Object.defineProperty(global, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // deprecated
removeListener: jest.fn(), // deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
window.matchMedia = (query) => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // Deprecated
removeListener: jest.fn(), // Deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
});

angular.module('grafana', ['ngRoute']);
Expand Down