1
1
import type { QRL } from '..' ;
2
2
import { hashCode } from '../shared/utils/hash_code' ;
3
3
import { OnRenderProp } from '../shared/utils/markers' ;
4
- import { isDomContainer } from '../client/dom-container' ;
5
- import type { SSRContainer } from '../ssr/ssr-types' ;
6
4
import { useSequentialScope } from './use-sequential-scope' ;
7
5
import { getNextUniqueIndex } from '../shared/utils/unique-index-generator' ;
8
6
@@ -12,13 +10,20 @@ export const useId = (): string => {
12
10
if ( val != null ) {
13
11
return val ;
14
12
}
15
- const containerBase = isDomContainer ( iCtx . $container$ )
16
- ? ''
17
- : ( iCtx . $container$ as SSRContainer ) . buildBase || '' ;
18
- const base = containerBase ? hashCode ( containerBase ) : '' ;
13
+ const containerBase = iCtx . $container$ . $buildBase$ || '' ;
14
+ const base = containerBase ? hashCode ( containerBase ) . substring ( 0 , 3 ) : '' ;
19
15
const componentQrl = iCtx . $container$ . getHostProp < QRL > ( iCtx . $hostElement$ , OnRenderProp ) ;
20
- const hash = componentQrl ?. getHash ( ) || '' ;
16
+ const hash = componentQrl ?. getHash ( ) . substring ( 0 , 3 ) || '' ;
21
17
const counter = getNextUniqueIndex ( iCtx . $container$ ) || '' ;
22
- const id = `${ base } -${ hash } -${ counter } ` ; // If no base and no hash, then "--#"
18
+ let id = `${ base } ${ hash } ${ counter } ` ;
19
+
20
+ let firstChar = id . charCodeAt ( 0 ) ;
21
+ // convert first char to letter if starts with a number, because CSS does not allow class names to start with a number
22
+ if ( firstChar >= 48 /* 0 */ && firstChar <= 57 /* 9 */ ) {
23
+ // 48 is char code for '0', 65 is char code for 'A'
24
+ // 65 - 48 = 17, so we add 17 to the char code of the first char to convert it to a letter
25
+ firstChar += 17 ;
26
+ id = String . fromCharCode ( firstChar ) + id . substring ( 1 ) ;
27
+ }
23
28
return set ( id ) ;
24
29
} ;
0 commit comments