Skip to content

Commit a936263

Browse files
Allow custom messages on DismissButton (#2868)
* Allow custom messages on DismissButton * Update packages/@react-aria/overlays/test/DismissButton.test.tsx Co-authored-by: Daniel Lu <[email protected]> Co-authored-by: Daniel Lu <[email protected]>
1 parent 1aa66e6 commit a936263

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

packages/@react-aria/overlays/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"@react-stately/overlays": "^3.1.5",
2626
"@react-types/button": "^3.4.3",
2727
"@react-types/overlays": "^3.5.3",
28+
"@react-types/shared": "^3.11.1",
2829
"dom-helpers": "^3.3.1"
2930
},
3031
"peerDependencies": {

packages/@react-aria/overlays/src/DismissButton.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13+
import {AriaLabelingProps, DOMProps} from '@react-types/shared';
1314
// @ts-ignore
1415
import intlMessages from '../intl/*.json';
1516
import React from 'react';
17+
import {useLabels} from '@react-aria/utils';
1618
import {useMessageFormatter} from '@react-aria/i18n';
1719
import {VisuallyHidden} from '@react-aria/visually-hidden';
1820

19-
interface DismissButtonProps {
21+
interface DismissButtonProps extends AriaLabelingProps, DOMProps {
2022
/** Called when the dismiss button is activated. */
2123
onDismiss?: () => void
2224
}
@@ -27,8 +29,11 @@ interface DismissButtonProps {
2729
* affordance to do so.
2830
*/
2931
export function DismissButton(props: DismissButtonProps) {
30-
let {onDismiss} = props;
32+
let {onDismiss, ...otherProps} = props;
3133
let formatMessage = useMessageFormatter(intlMessages);
34+
35+
let labels = useLabels(otherProps, formatMessage('dismiss'));
36+
3237
let onClick = () => {
3338
if (onDismiss) {
3439
onDismiss();
@@ -38,8 +43,8 @@ export function DismissButton(props: DismissButtonProps) {
3843
return (
3944
<VisuallyHidden>
4045
<button
46+
{...labels}
4147
tabIndex={-1}
42-
aria-label={formatMessage('dismiss')}
4348
onClick={onClick} />
4449
</VisuallyHidden>
4550
);
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2020 Adobe. All rights reserved.
3+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License. You may obtain a copy
5+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under
8+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
* OF ANY KIND, either express or implied. See the License for the specific language
10+
* governing permissions and limitations under the License.
11+
*/
12+
13+
import {DismissButton} from '..';
14+
import React from 'react';
15+
import {render} from '@testing-library/react';
16+
17+
18+
describe('DismissButton', function () {
19+
it('should have a default aria-label', function () {
20+
let {getByRole} = render(<DismissButton />);
21+
expect(getByRole('button', {hidden: true})).toHaveAttribute('aria-label', 'Dismiss');
22+
});
23+
24+
it('should accept an aria-label', function () {
25+
let {getByRole} = render(<DismissButton aria-label="foo" />);
26+
expect(getByRole('button', {hidden: true})).toHaveAttribute('aria-label', 'foo');
27+
});
28+
29+
it('should accept an aria-labelledby', function () {
30+
let {getByRole} = render(
31+
<>
32+
<span id="span-id">bar</span>
33+
<DismissButton aria-labelledby="span-id" />
34+
</>
35+
);
36+
expect(getByRole('button', {hidden: true})).toHaveAttribute('aria-labelledby', 'span-id');
37+
expect(getByRole('button', {hidden: true})).not.toHaveAttribute('aria-label');
38+
});
39+
40+
it('should accept an aria-labelledby and aria-label', function () {
41+
let {getByRole} = render(
42+
<>
43+
<span id="span-id">bar</span>
44+
<DismissButton aria-labelledby="span-id" aria-label="foo" id="self" />
45+
</>
46+
);
47+
expect(getByRole('button', {hidden: true})).toHaveAttribute('aria-labelledby', 'span-id self');
48+
expect(getByRole('button', {hidden: true})).toHaveAttribute('aria-label', 'foo');
49+
});
50+
51+
});

0 commit comments

Comments
 (0)