Skip to content

Commit 3b8c072

Browse files
authored
ColorSwatch - fix accessibility (#3510)
* ColorSwatch - fix accessibility
1 parent f854b30 commit 3b8c072

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

demo/src/screens/componentScreens/ColorSwatchScreen.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ export default class ColorSwatchScreen extends Component {
7373
<Text text90R>Unavailable</Text>
7474
</View>
7575
<View>
76-
<ColorSwatch transparent/>
76+
<ColorSwatch color={'transparent'}/>
7777
<Text text90R>Transparent</Text>
7878
</View>
7979
<View center>
80-
<ColorSwatch style={{borderRadius: 0}} transparent/>
80+
<ColorSwatch style={{borderRadius: 0}} transparent selected/>
8181
<Text text90R>Square</Text>
8282
</View>
8383
</View>

src/components/colorSwatch/index.tsx

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ const transparentImage = require('./assets/transparentSwatch/TransparentSwatch.p
5656
const DEFAULT_SIZE = Constants.isTablet ? 44 : 36;
5757
export const SWATCH_MARGIN = 12;
5858
export const SWATCH_SIZE = DEFAULT_SIZE;
59+
const DEFAULT_COLOR = Colors.grey30;
5960

6061
/**
6162
* @description: A color swatch component
@@ -124,9 +125,8 @@ class ColorSwatch extends PureComponent<Props & BaseComponentInjectedProps> {
124125

125126
onPress = () => {
126127
const {value, index} = this.props;
127-
const color = this.color ?? '';
128128
const tintColor = this.getTintColor(value);
129-
const result = value || color;
129+
const result = value || this.color || '';
130130
const hexString = Colors.getHexString(result);
131131
this.props.onPress?.(result, {tintColor, index, hexString});
132132
};
@@ -141,11 +141,13 @@ class ColorSwatch extends PureComponent<Props & BaseComponentInjectedProps> {
141141
}
142142

143143
getAccessibilityInfo() {
144-
const color = this.color;
145-
144+
const color = this.color || DEFAULT_COLOR;
145+
const defaultText = !this.color ? 'default' : '';
146+
146147
return {
147-
accessibilityLabel: color && Colors.getColorName(color),
148-
accessibilityStates: this.props.selected ? ['selected'] : []
148+
accessible: true,
149+
accessibilityLabel: `${defaultText} color ${Colors.getColorName(color)}`,
150+
accessibilityState: {selected: this.props.selected}
149151
};
150152
}
151153

@@ -159,11 +161,9 @@ class ColorSwatch extends PureComponent<Props & BaseComponentInjectedProps> {
159161

160162
renderContent() {
161163
const {style, onPress, unavailable, size = DEFAULT_SIZE, ...others} = this.props;
162-
const color = this.color;
163164
const {isSelected} = this.state;
164165
const Container = onPress ? TouchableOpacity : View;
165-
const tintColor = this.getTintColor(color);
166-
const accessibilityInfo = Constants.accessibility.isScreenReaderEnabled && this.getAccessibilityInfo();
166+
const tintColor = this.getTintColor(this.color);
167167

168168
return (
169169
<Container
@@ -175,9 +175,9 @@ class ColorSwatch extends PureComponent<Props & BaseComponentInjectedProps> {
175175
onPress={this.onPress}
176176
style={[this.styles.container, {width: size, height: size, borderRadius: size / 2}, style]}
177177
onLayout={this.onLayout}
178-
{...accessibilityInfo}
178+
{...this.getAccessibilityInfo()}
179179
>
180-
{Colors.isTransparent(color) && (
180+
{Colors.isTransparent(this.color) && (
181181
<Image source={transparentImage} style={this.styles.transparentImage} resizeMode={'cover'}/>
182182
)}
183183
{unavailable ? (
@@ -222,11 +222,11 @@ class ColorSwatch extends PureComponent<Props & BaseComponentInjectedProps> {
222222

223223
export default asBaseComponent<ColorSwatchProps>(ColorSwatch);
224224

225-
function createStyles({color = Colors.grey30}) {
225+
function createStyles({color = DEFAULT_COLOR}) {
226226
return StyleSheet.create({
227227
container: {
228228
backgroundColor: color,
229-
borderWidth: color === 'transparent' ? undefined : 1,
229+
borderWidth: Colors.isTransparent(color) ? undefined : 1,
230230
borderColor: Colors.rgba(Colors.$outlineDisabledHeavy, 0.2),
231231
margin: SWATCH_MARGIN,
232232
overflow: 'hidden'

src/style/colors.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ export class Colors {
162162
}
163163

164164
getColorName(colorValue: string) {
165+
if (this.isTransparent(colorValue)) {
166+
return 'transparent';
167+
}
165168
const color = colorStringValue(colorValue);
166169
return ColorName.name(color)[1];
167170
}

0 commit comments

Comments
 (0)