Skip to content

Commit 2679d1a

Browse files
author
Kolja
committed
fix(a11y): WCAG 2.4.6 — fix duplicate Filters heading, adopt shared VisuallyHidden
Addresses six review concerns on PR #39241: - Bug: vertical filter bar rendered "Filters" h2 twice (once outside the ResizableSidebar wrapper, once inside renderChild before StickyPanel). Screen readers announced the heading twice. Removed the outer one and kept the inner one — that's the semantically correct location since it scopes to the FiltersPanel container. - DRY: replaced the SrOnlyH1 / SrOnlyH2 styled blocks copy-pasted into five files with the shared VisuallyHidden component (with `as` prop). Component lives in src/components/Accessibility/ alongside SkipLink. - ChartCreation no longer renders the title twice (sr-only h1 plus visible aria-hidden h2). Now uses a single visible h1 styled to match the previous h2 size, with the StyledContainer CSS rule updated from h2 to h1. - StyledContainer CSS audit: no other selectors target ChartCreation h3 anywhere in src — the previous h3 → h2 → h1 chain is contained. - Indentation in DashboardWrapper return cleaned up (no fragment wrapper needed after removing the duplicate h2). - Tests added for VisuallyHidden covering default rendering, custom `as`, prop forwarding, and the sr-only style invariant. This PR depends on #39240 introducing the same Accessibility folder; once that merges first, the duplicate component definition will rebase away.
1 parent 179d7b0 commit 2679d1a

8 files changed

Lines changed: 155 additions & 72 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
import { render, screen } from 'spec/helpers/testing-library';
20+
import VisuallyHidden from './VisuallyHidden';
21+
22+
test('renders children', () => {
23+
render(<VisuallyHidden>Screen-reader only text</VisuallyHidden>);
24+
expect(screen.getByText('Screen-reader only text')).toBeInTheDocument();
25+
});
26+
27+
test('renders as span by default', () => {
28+
render(<VisuallyHidden>Default tag</VisuallyHidden>);
29+
const el = screen.getByText('Default tag');
30+
expect(el.tagName).toBe('SPAN');
31+
});
32+
33+
test('renders as a custom element when as prop is provided', () => {
34+
render(<VisuallyHidden as="h1">Page heading</VisuallyHidden>);
35+
const el = screen.getByText('Page heading');
36+
expect(el.tagName).toBe('H1');
37+
});
38+
39+
test('forwards id and className props', () => {
40+
render(
41+
<VisuallyHidden id="my-id" className="my-class">
42+
Text
43+
</VisuallyHidden>,
44+
);
45+
const el = screen.getByText('Text');
46+
expect(el).toHaveAttribute('id', 'my-id');
47+
expect(el).toHaveClass('my-class');
48+
});
49+
50+
test('applies clip-rect style for screen-reader-only behavior', () => {
51+
render(<VisuallyHidden>Hidden</VisuallyHidden>);
52+
const el = screen.getByText('Hidden');
53+
expect(el).toHaveStyle({ position: 'absolute' });
54+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
import { ElementType, FC, ReactNode } from 'react';
20+
import { styled } from '@apache-superset/core/theme';
21+
22+
/**
23+
* VisuallyHidden — content that is available to assistive technology but not
24+
* visually rendered. Use for screen-reader-only headings, labels, and live
25+
* regions where a duplicate visible element would be redundant.
26+
*
27+
* Renders a `<span>` by default. Pass `as` to render a different element
28+
* (e.g. `as="h1"` for an sr-only page heading).
29+
*/
30+
const HiddenElement = styled.span`
31+
position: absolute;
32+
width: 1px;
33+
height: 1px;
34+
padding: 0;
35+
margin: -1px;
36+
overflow: hidden;
37+
clip: rect(0, 0, 0, 0);
38+
white-space: nowrap;
39+
border: 0;
40+
`;
41+
42+
export interface VisuallyHiddenProps {
43+
/** Element type to render. Defaults to `'span'`. */
44+
as?: ElementType;
45+
children?: ReactNode;
46+
id?: string;
47+
className?: string;
48+
}
49+
50+
const VisuallyHidden: FC<VisuallyHiddenProps> = ({
51+
as = 'span',
52+
children,
53+
...rest
54+
}) => (
55+
<HiddenElement as={as} {...rest}>
56+
{children}
57+
</HiddenElement>
58+
);
59+
60+
export default VisuallyHidden;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
export {
20+
default as VisuallyHidden,
21+
type VisuallyHiddenProps,
22+
} from './VisuallyHidden';

‎superset-frontend/src/dashboard/components/DashboardBuilder/DashboardBuilder.tsx‎

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { css, styled, useTheme } from '@apache-superset/core/theme';
2525
import { useDispatch, useSelector } from 'react-redux';
2626
import { EmptyState, Loading } from '@superset-ui/core/components';
2727
import { ErrorBoundary, BasicErrorAlert } from 'src/components';
28+
import { VisuallyHidden } from 'src/components/Accessibility';
2829
import BuilderComponentPane from 'src/dashboard/components/BuilderComponentPane';
2930
import DashboardHeader from 'src/dashboard/components/Header';
3031
import { Icons } from '@superset-ui/core/components/Icons';
@@ -351,17 +352,6 @@ const StyledDashboardContent = styled.div<{
351352
`}
352353
`;
353354

354-
const SrOnlyH2 = styled.h2`
355-
position: absolute;
356-
width: 1px;
357-
height: 1px;
358-
padding: 0;
359-
margin: -1px;
360-
overflow: hidden;
361-
clip: rect(0, 0, 0, 0);
362-
white-space: nowrap;
363-
border: 0;
364-
`;
365355

366356
const ELEMENT_ON_SCREEN_OPTIONS = {
367357
threshold: [1],
@@ -522,7 +512,7 @@ const DashboardBuilder = () => {
522512
{showFilterBar &&
523513
filterBarOrientation === FilterBarOrientation.Horizontal && (
524514
<>
525-
<SrOnlyH2>{t('Filters')}</SrOnlyH2>
515+
<VisuallyHidden as="h2">{t('Filters')}</VisuallyHidden>
526516
<FilterBar
527517
orientation={FilterBarOrientation.Horizontal}
528518
hidden={isReport}
@@ -596,7 +586,7 @@ const DashboardBuilder = () => {
596586
hidden={isReport}
597587
data-test="dashboard-filters-panel"
598588
>
599-
<SrOnlyH2>{t('Filters')}</SrOnlyH2>
589+
<VisuallyHidden as="h2">{t('Filters')}</VisuallyHidden>
600590
<StickyPanel ref={containerRef} width={filterBarWidth}>
601591
<ErrorBoundary>
602592
<FilterBar
@@ -632,8 +622,6 @@ const DashboardBuilder = () => {
632622
return (
633623
<DashboardWrapper>
634624
{isVerticalFilterBarVisible && (
635-
<>
636-
<SrOnlyH2>{t('Filters')}</SrOnlyH2>
637625
<ResizableSidebar
638626
id={`dashboard:${dashboardId}`}
639627
enable={dashboardFiltersOpen}
@@ -643,7 +631,6 @@ const DashboardBuilder = () => {
643631
>
644632
{renderChild}
645633
</ResizableSidebar>
646-
</>
647634
)}
648635
<StyledHeader
649636
data-test="dashboard-header-wrapper"
@@ -668,7 +655,7 @@ const DashboardBuilder = () => {
668655
{renderDraggableContent}
669656
</Droppable>
670657
</StyledHeader>
671-
<SrOnlyH2>{t('Dashboard content')}</SrOnlyH2>
658+
<VisuallyHidden as="h2">{t('Dashboard content')}</VisuallyHidden>
672659
<StyledContent fullSizeChartId={fullSizeChartId}>
673660
{!editMode &&
674661
!topLevelTabs &&

‎superset-frontend/src/dashboard/components/Header/index.tsx‎

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { t } from '@apache-superset/core/translation';
2828
import { Global } from '@emotion/react';
2929
import { shallowEqual, useDispatch, useSelector } from 'react-redux';
3030
import { bindActionCreators } from 'redux';
31+
import { VisuallyHidden } from 'src/components/Accessibility';
3132
import { LOG_ACTIONS_TOGGLE_EDIT_DASHBOARD } from 'src/logger/LogUtils';
3233
import { Icons } from '@superset-ui/core/components/Icons';
3334
import {
@@ -177,18 +178,6 @@ const actionButtonsStyle = (theme: SupersetTheme) => css`
177178
}
178179
`;
179180

180-
const SrOnlyH1 = styled.h1`
181-
position: absolute;
182-
width: 1px;
183-
height: 1px;
184-
padding: 0;
185-
margin: -1px;
186-
overflow: hidden;
187-
clip: rect(0, 0, 0, 0);
188-
white-space: nowrap;
189-
border: 0;
190-
`;
191-
192181
const StyledUndoRedoButton = styled(Button)`
193182
// TODO: check if we need this
194183
padding: 0;
@@ -839,7 +828,9 @@ const Header = (): JSX.Element => {
839828
data-test-id={dashboardInfo.id}
840829
className="dashboard-header-container"
841830
>
842-
<SrOnlyH1>{dashboardTitle || t('Dashboard')}</SrOnlyH1>
831+
<VisuallyHidden as="h1">
832+
{dashboardTitle || t('Dashboard')}
833+
</VisuallyHidden>
843834
<PageHeaderWithActions
844835
editableTitleProps={editableTitleProps}
845836
certificatiedBadgeProps={certifiedBadgeProps}

‎superset-frontend/src/explore/components/ExploreChartHeader/index.tsx‎

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ import {
2727
UnsavedChangesModal,
2828
} from '@superset-ui/core/components';
2929
import { AlteredSliceTag } from 'src/components';
30+
import { VisuallyHidden } from 'src/components/Accessibility';
3031
import {
3132
SupersetClient,
3233
isMatrixifyEnabled,
3334
MatrixifyFormData,
3435
} from '@superset-ui/core';
3536
import { logging } from '@apache-superset/core/utils';
36-
import { css, styled, SupersetTheme } from '@apache-superset/core/theme';
37+
import { css, SupersetTheme } from '@apache-superset/core/theme';
3738
import { t } from '@apache-superset/core/translation';
3839
import { Icons } from '@superset-ui/core/components/Icons';
3940
import PropertiesModal from 'src/explore/components/PropertiesModal';
@@ -84,18 +85,6 @@ export interface ExploreChartHeaderProps {
8485
isSaveModalVisible?: boolean;
8586
}
8687

87-
const SrOnlyH1 = styled.h1`
88-
position: absolute;
89-
width: 1px;
90-
height: 1px;
91-
padding: 0;
92-
margin: -1px;
93-
overflow: hidden;
94-
clip: rect(0, 0, 0, 0);
95-
white-space: nowrap;
96-
border: 0;
97-
`;
98-
9988
const saveButtonStyles = (theme: SupersetTheme) => css`
10089
color: ${theme.colorPrimaryText};
10190
& > span[role='img'] {
@@ -284,7 +273,7 @@ export const ExploreChartHeader: FC<ExploreChartHeaderProps> = ({
284273

285274
return (
286275
<>
287-
<SrOnlyH1>{sliceName || t('Explore chart')}</SrOnlyH1>
276+
<VisuallyHidden as="h1">{sliceName || t('Explore chart')}</VisuallyHidden>
288277
<PageHeaderWithActions
289278
editableTitleProps={{
290279
title: sliceName ?? '',

‎superset-frontend/src/pages/ChartCreation/index.tsx‎

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,12 @@ const StyledContainer = styled.div`
8484
padding-right: ${theme.padding}px;
8585
padding-bottom: ${theme.padding}px;
8686
87-
h2 {
87+
h1 {
8888
padding-bottom: ${theme.paddingSM}px;
89+
font-size: ${theme.fontSizeXL}px;
90+
font-weight: ${theme.fontWeightStrong};
91+
line-height: ${theme.lineHeightHeading2};
92+
margin: 0;
8993
}
9094
9195
& .dataset {
@@ -158,18 +162,6 @@ const StyledContainer = styled.div`
158162
`}
159163
`;
160164

161-
const SrOnlyH1 = styled.h1`
162-
position: absolute;
163-
width: 1px;
164-
height: 1px;
165-
padding: 0;
166-
margin: -1px;
167-
overflow: hidden;
168-
clip: rect(0, 0, 0, 0);
169-
white-space: nowrap;
170-
border: 0;
171-
`;
172-
173165
const StyledStepTitle = styled.span`
174166
${({ theme: { fontSize, fontWeightStrong } }) => `
175167
font-size: ${fontSize}px;
@@ -341,8 +333,7 @@ export class ChartCreation extends PureComponent<
341333

342334
return (
343335
<StyledContainer>
344-
<SrOnlyH1>{t('Create a new chart')}</SrOnlyH1>
345-
<h2 aria-hidden="true">{t('Create a new chart')}</h2>
336+
<h1>{t('Create a new chart')}</h1>
346337
<Steps direction="vertical" size="small">
347338
<Steps.Step
348339
title={<StyledStepTitle>{t('Choose a dataset')}</StyledStepTitle>}

‎superset-frontend/src/pages/Home/index.tsx‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import { Switch } from '@superset-ui/core/components/Switch';
4949
import getBootstrapData from 'src/utils/getBootstrapData';
5050
import { TableTab } from 'src/views/CRUD/types';
5151
import SubMenu, { SubMenuProps } from 'src/features/home/SubMenu';
52+
import { VisuallyHidden } from 'src/components/Accessibility';
5253
import { userHasPermission } from 'src/dashboard/util/permissionUtils';
5354
import { WelcomePageLastTab } from 'src/features/home/types';
5455
import ActivityTable from 'src/features/home/ActivityTable';
@@ -76,18 +77,6 @@ interface LoadingProps {
7677

7778
const DEFAULT_TAB_ARR = ['dashboards', 'charts'];
7879

79-
const SrOnlyH1 = styled.h1`
80-
position: absolute;
81-
width: 1px;
82-
height: 1px;
83-
padding: 0;
84-
margin: -1px;
85-
overflow: hidden;
86-
clip: rect(0, 0, 0, 0);
87-
white-space: nowrap;
88-
border: 0;
89-
`;
90-
9180
const WelcomeContainer = styled.div`
9281
background: ${({ theme }) => theme.colorBgLayout};
9382
.ant-row.menu {
@@ -362,7 +351,7 @@ function Welcome({ user, addDangerToast }: WelcomeProps) {
362351
<SubMenu {...menuData} />
363352
)}
364353
<WelcomeContainer>
365-
<SrOnlyH1>{t('Home')}</SrOnlyH1>
354+
<VisuallyHidden as="h1">{t('Home')}</VisuallyHidden>
366355
{WelcomeMessageExtension && <WelcomeMessageExtension />}
367356
{WelcomeTopExtension && <WelcomeTopExtension />}
368357
{WelcomeMainExtension && <WelcomeMainExtension />}

0 commit comments

Comments
 (0)