Skip to content

Commit b20fa2b

Browse files
authored
feat(core): add isAccessible util (#593)
1 parent cf3ebaa commit b20fa2b

4 files changed

Lines changed: 96 additions & 1 deletion

File tree

‎.changeset/tall-doors-smile.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clack/core': minor
3+
---
4+
5+
Add `isAccessible()` and an `accessible` setting as the foundation for accessible mode. Resolution order: per-call option > `updateSettings({ accessible })` > the `ACCESSIBLE` environment variable (any non-empty value enables it, except `0` and `false`).

‎packages/core/src/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ export {
3030
wrapTextWithPrefix,
3131
} from './utils/index.js';
3232
export type { ClackSettings } from './utils/settings.js';
33-
export { settings, updateSettings } from './utils/settings.js';
33+
export { isAccessible, settings, updateSettings } from './utils/settings.js';
3434
export type { Validate } from './utils/validation.js';
3535
export { runValidation } from './utils/validation.js';

‎packages/core/src/utils/settings.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ interface InternalClackSettings {
2525
error: string;
2626
};
2727
withGuide: boolean;
28+
accessible: boolean | undefined;
2829
date: {
2930
monthNames: string[];
3031
messages: {
@@ -54,6 +55,7 @@ export const settings: InternalClackSettings = {
5455
error: 'Something went wrong',
5556
},
5657
withGuide: true,
58+
accessible: undefined,
5759
date: {
5860
monthNames: [...DEFAULT_MONTH_NAMES],
5961
messages: {
@@ -94,6 +96,12 @@ export interface ClackSettings {
9496

9597
withGuide?: boolean;
9698

99+
/**
100+
* Force accessible (static, screen-reader friendly) output on or off.
101+
* Overrides the `ACCESSIBLE` environment variable.
102+
*/
103+
accessible?: boolean;
104+
97105
/**
98106
* Date prompt localization
99107
*/
@@ -145,6 +153,10 @@ export function updateSettings(updates: ClackSettings) {
145153
settings.withGuide = updates.withGuide !== false;
146154
}
147155

156+
if (updates.accessible !== undefined) {
157+
settings.accessible = updates.accessible === true;
158+
}
159+
148160
if (updates.date !== undefined) {
149161
const date = updates.date;
150162
if (date.monthNames !== undefined) {
@@ -170,6 +182,21 @@ export function updateSettings(updates: ClackSettings) {
170182
}
171183
}
172184

185+
/**
186+
* Resolves whether accessible (static, screen-reader friendly) output is enabled.
187+
*
188+
* Resolution order: per-call option > global `accessible` setting > `ACCESSIBLE`
189+
* environment variable (any non-empty value enables it, except `0` and `false`).
190+
*
191+
* @param accessible - Per-call override; takes precedence when defined
192+
*/
193+
export function isAccessible(accessible?: boolean): boolean {
194+
if (accessible !== undefined) return accessible;
195+
if (settings.accessible !== undefined) return settings.accessible;
196+
const value = process.env.ACCESSIBLE;
197+
return value !== undefined && value !== '' && value !== '0' && value !== 'false';
198+
}
199+
173200
/**
174201
* Check if a key is an alias for a default action
175202
* @param key - The raw key which might match to an action
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { afterEach, describe, expect, test } from 'vitest';
2+
import { isAccessible, settings, updateSettings } from '../../src/utils/settings.js';
3+
4+
describe('isAccessible', () => {
5+
const originalEnv = process.env.ACCESSIBLE;
6+
7+
afterEach(() => {
8+
if (originalEnv === undefined) {
9+
delete process.env.ACCESSIBLE;
10+
} else {
11+
process.env.ACCESSIBLE = originalEnv;
12+
}
13+
settings.accessible = undefined;
14+
});
15+
16+
describe('ACCESSIBLE env var', () => {
17+
test('disabled when unset', () => {
18+
delete process.env.ACCESSIBLE;
19+
expect(isAccessible()).toBe(false);
20+
});
21+
22+
test.each(['', '0', 'false'])('disabled when set to %j', (value) => {
23+
process.env.ACCESSIBLE = value;
24+
expect(isAccessible()).toBe(false);
25+
});
26+
27+
test.each(['1', 'true', 'yes'])('enabled when set to %j', (value) => {
28+
process.env.ACCESSIBLE = value;
29+
expect(isAccessible()).toBe(true);
30+
});
31+
});
32+
33+
describe('global setting', () => {
34+
test('updateSettings({ accessible: true }) enables it', () => {
35+
delete process.env.ACCESSIBLE;
36+
updateSettings({ accessible: true });
37+
expect(isAccessible()).toBe(true);
38+
});
39+
40+
test('updateSettings({ accessible: false }) overrides the env var', () => {
41+
process.env.ACCESSIBLE = '1';
42+
updateSettings({ accessible: false });
43+
expect(isAccessible()).toBe(false);
44+
});
45+
});
46+
47+
describe('per-call option', () => {
48+
test('true overrides a false global setting', () => {
49+
updateSettings({ accessible: false });
50+
expect(isAccessible(true)).toBe(true);
51+
});
52+
53+
test('false overrides the env var', () => {
54+
process.env.ACCESSIBLE = '1';
55+
expect(isAccessible(false)).toBe(false);
56+
});
57+
58+
test('undefined falls through to env var', () => {
59+
process.env.ACCESSIBLE = '1';
60+
expect(isAccessible(undefined)).toBe(true);
61+
});
62+
});
63+
});

0 commit comments

Comments
 (0)