An open source design system that's fully customizable and agent ready
TypeScript
fix(Avatar): draw initials from letters, digits and emoji, not punctuation (#6698)
Merlin7ada42ea8a59
diff --git a/.changeset/avatar-initials-skip-punctuation.md b/.changeset/avatar-initials-skip-punctuation.mdnew file mode 100644index 0000000000..95d23f24fc--- /dev/null+++ b/.changeset/avatar-initials-skip-punctuation.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] Avatar initials skip punctuation: each word's initial is its first letter, digit or emoji, so `Northwind Workbench (automation)` renders `NA` instead of `N(` and `“Ada” Lovelace` renders `AL` instead of `“L`. Words with none of those, such as a lone `-`, are ignored, and a name made only of punctuation shows the default icon rather than a stray symbol.++@light-merlin-darkdiff --git a/packages/core/src/Avatar/Avatar.doc.mjs b/packages/core/src/Avatar/Avatar.doc.mjsindex fc261f17bc..59aff2ac17 100644--- a/packages/core/src/Avatar/Avatar.doc.mjs+++ b/packages/core/src/Avatar/Avatar.doc.mjs@@ -23,7 +23,7 @@ export const docs = {],anatomy: [{name: 'Photo', required: false, description: 'The profile image, loaded from the src URL. Shown when available.'},- {name: 'Initials', required: false, description: 'One or two letters extracted from the name. Shown when no photo is available.'},+ {name: 'Initials', required: false, description: 'The first letter, digit or emoji of the first and last words of the name; punctuation is skipped. Shown when no photo is available. A name with no letters, digits or emoji shows the default icon instead.'},{name: 'Default icon', required: false, description: 'A generic person silhouette. Shown when there is no photo or name.'},{name: 'Status dot', required: false, description: 'A small indicator in the bottom-right corner showing availability (online, away, busy). Each variant pairs colour with a distinct shape so status does not rely on colour alone.'},],diff --git a/packages/core/src/Avatar/Avatar.test.tsx b/packages/core/src/Avatar/Avatar.test.tsxindex 63d116e913..0a7a17d49b 100644--- a/packages/core/src/Avatar/Avatar.test.tsx+++ b/packages/core/src/Avatar/Avatar.test.tsx@@ -103,6 +103,36 @@ describe('Avatar', () => {expect(screen.getByTestId('avatar')).toHaveTextContent('👨👩👧👦A');});+ describe('initials come from letters, digits and emoji, not punctuation', () => {+ it.each([+ ['Northwind Workbench (automation)', 'NA'],+ ['Ada "The Countess" Lovelace', 'AL'],+ ['“Ada” Lovelace', 'AL'],+ ['Ada - Lovelace', 'AL'],+ ['Ada Lovelace —', 'AL'],+ ['Mary-Jane Watson', 'MW'],+ ['(Ada)', 'A'],+ ['Alice', 'A'],+ ['R2 D2', 'RD'],+ ['3M', '3'],+ ['émile zola', 'ÉZ'],+ ['😀 Ada', '😀A'],+ ['Ada (😀)', 'A😀'],+ ])('%s renders %s', (name, expected) => {+ render(<Avatar name={name} data-testid="avatar" />);+ expect(screen.getByTestId('avatar').textContent).toBe(expected);+ });++ it('falls through to the default icon when no word yields an initial', () => {+ render(<Avatar name="- ()" data-testid="a" />);+ const el = screen.getByTestId('a');+ expect(el.querySelector('svg')).not.toBeNull();+ expect(el).toHaveTextContent('');+ // The name still names the avatar.+ expect(screen.getByRole('img', {name: '- ()'})).toBeInTheDocument();+ });+ });+it('retries a new src after a previous src failed to load', () => {const {rerender} = render(<Avatar name="Ada" src="https://example.com/broken.jpg" />,diff --git a/packages/core/src/Avatar/Avatar.tsx b/packages/core/src/Avatar/Avatar.tsxindex fa94ce0904..084c5c4ecb 100644--- a/packages/core/src/Avatar/Avatar.tsx+++ b/packages/core/src/Avatar/Avatar.tsx@@ -384,25 +384,46 @@ export interface AvatarProps extends BaseProps<HTMLDivElement> {}/**- * Generates initials from a name string.- * Takes the first letter of the first two words.+ * A character that can stand for a word in initials: a letter, a digit, or an+ * emoji. Punctuation and other symbols, such as `(`, `"` or `-`, cannot.+ */+const INITIAL_CHARACTER =+ /[\p{L}\p{N}\p{Extended_Pictographic}\p{Regional_Indicator}]/u;++/**+ * The first character of a word that can stand for it in initials, skipping+ * leading punctuation, or '' when the word has none (a lone `-` or `()`).+ */+function initialCharacter(word: string): string {+ const index = word.search(INITIAL_CHARACTER);+ return index === -1 ? '' : firstCharacter(word.slice(index));+}++/**+ * Generates initials from a name string: the first letter, digit or emoji of+ * the first and last words. Words with none of those, such as a lone `-`, are+ * skipped, and leading punctuation within a word is ignored. Returns '' when+ * no word yields an initial.* @example* ```- * getInitials('John Doe')- * getInitials('Alice')+ * getInitials('John Doe') // 'JD'+ * getInitials('Alice') // 'A'+ * getInitials('Northwind Workbench (automation)') // 'NA'* ```*/function getInitials(name: string): string {- const words = name.trim().split(/\s+/);- if (words.length === 0) {+ const initials = name+ .trim()+ .split(/\s+/)+ .map(initialCharacter)+ .filter(initial => initial !== '');+ if (initials.length === 0) {return '';}- if (words.length === 1) {- return firstCharacter(words[0]).toUpperCase();+ if (initials.length === 1) {+ return initials[0].toUpperCase();}- return (- firstCharacter(words[0]) + firstCharacter(words[words.length - 1])- ).toUpperCase();+ return (initials[0] + initials[initials.length - 1]).toUpperCase();}/**@@ -554,8 +575,11 @@ export function Avatar({// truthy), leaving an empty plate behind a blank accessible name.const meaningfulName = meaningful(name);const meaningfulAlt = meaningful(alt);- const showInitials = !showImage && !showFallbackImage && meaningfulName;- const showIcon = !showImage && !showFallbackImage && !meaningfulName;+ // A name made only of punctuation ("—", "()") still names the avatar, but+ // yields no initials; it falls back to the default icon, not an empty plate.+ const initials = meaningfulName ? getInitials(meaningfulName) : '';+ const showInitials = !showImage && !showFallbackImage && initials !== '';+ const showIcon = !showImage && !showFallbackImage && initials === '';// A meaningful accessible name comes from `alt`/`name`, composed with the// status element's `label` when one is present ("Jane Doe, Online") — the@@ -753,7 +777,7 @@ export function Avatar({dynamicStyles.fontSize(numericSize),),)}>- {getInitials(meaningfulName)}+ {initials}</div>)}{showIcon && (