Skip to main content
Elastic UI
Elastic UI
Getting startedComponentsPatternsContentData visualization
EUI ChangelogGitHubFigma
  • Setup
  • Theming
  • Accessibility
  • Testing
  • Utilities
    • Accessibility
    • Auto sizer
    • Color palettes
    • Copy
    • CSS utility classes
    • Delay
    • Error boundary
    • Focus trap
    • Highlight and mark
    • HTML ID generator
    • I18n
    • Inner text
    • Mutation observer
    • Outside click detector
    • Overlay mask
    • Portal
    • Pretty duration
    • Provider
    • Resize observer
    • Scroll
    • Text diff
    • Text truncation
    • Window events
  • EUI
  • Utilities
  • HTML ID generator

HTML ID generator

Use

htmlIdGenerator()() to generate unique IDs for elements with an optional prefix and/or suffix. The first call to htmlIdGenerator accepts the prefix as an optional argument and returns a second function which accepts an optional suffix and returns the generated ID.

Loading...

ID generator with prefix

Provide a prefix to the generator to get an ID that starts with the specified prefix.

Loading...

ID generator with suffix

Provide a suffix to the generator to get an ID that starts with the specified suffix.

Loading...

ID generator with both prefix and suffix

The htmlIdGenerator is capable of generating an ID with both a specified prefix and suffix.

Loading...

Reusing the generator for multiple IDs

As you may have noticed, htmlIdGenerator is a curried function. This means you can reuse the original htmlIdGenerator() call to generate multiple IDs. Additionally, if you pass in suffixes to your second call, the generated ID(s) will share the same unique ID.

Loading...

Memoized hook for component use

useGeneratedHtmlId is a custom React hook that automatically memoizes a randomly generated ID, preventing the ID from regenerating on every component rerender. The ID will only change if the component fully unmounts/mounts, or if you dynamically pass in new hook arguments.

Please note that unlike htmlIdGenerator, useGeneratedHtmlId is a single function and does not support generating multiple IDs that share the same unique ID. It is instead best used for simple one-off IDs, rather than groups of them.

Loading...

Props

useGeneratedHtmlId

๐˜‚๐˜‚
โœ„๐˜—โœ„๐˜—
Prop
โ†ฆ
Description and type
โ†ฆ
Default value
โ†ต
Prop
prefix#
โ†ฆ
Description and type

Optional prefix to prepend to the generated ID

Type: string
โ†ฆ
Default value
โ†ต
Prop
suffix#
โ†ฆ
Description and type

Optional suffix to append to the generated ID

Type: string
โ†ฆ
Default value
โ†ต
Prop
conditionalId#
โ†ฆ
Description and type

Optional conditional ID to use instead of a randomly generated ID.
Typically used by EUI components where IDs can be passed in as custom props

Type: string
โ†ฆ
Default value
โ†ต
๐˜‚๐˜‚
Edit this page

Previous
Highlight and mark
Next
I18n
  • ID generator with prefix
  • ID generator with suffix
  • ID generator with both prefix and suffix
  • Reusing the generator for multiple IDs
  • Memoized hook for component use
  • Props
EUI is dual-licensed under Elastic License 2.0 and Server Side Public License, v 1 | Crafted with โค by Elastic