components icon indicating copy to clipboard operation
components copied to clipboard

fix(multiple): include APP_ID in element IDs

Open jelbourn opened this issue 1 year ago • 3 comments
trafficstars

Many components generate element IDs, which are used primarily for accessible labeling. The IDs all use an incrementing number concatenated with some stirng specific to that component. This change add Angular's APP_ID into these IDs in order to avoid ID collisions in cases where there are multiple instances of Angular running on the same page.

jelbourn avatar Jun 14 '24 20:06 jelbourn

Yeah, I could see having something like

let nextId = 0;

@Injectable()
class IdGenerator {
  appId = inject(APP_ID);
  generateId(prefix: string) {
    return `${prefix}${this.appId}${nextId++}`;
  }
}

I guess in cdk/a11y ? Not sure there's a more appropriate place for it

jelbourn avatar Jun 14 '24 21:06 jelbourn

Yeah I think cdk/a11y would be appropriate for it.

crisbeto avatar Jun 15 '24 08:06 crisbeto

Updated the PR to include an IdGenerator class (in its own commit) and then use that class in most of the ID generation. I intentionally left stepper and tab-group because they do something slightly different

jelbourn avatar Jun 18 '24 21:06 jelbourn

Closing in favor of https://github.com/angular/components/pull/29948.

crisbeto avatar Nov 19 '24 09:11 crisbeto

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.