This repository was archived by the owner on Mar 4, 2024. It is now read-only.
File tree 2 files changed +25
-5
lines changed 2 files changed +25
-5
lines changed Original file line number Diff line number Diff line change 1
- import React , { useEffect , useMemo , useState } from 'react'
1
+ import React , { useMemo } from 'react'
2
2
3
3
import styled from 'styled-components'
4
4
import { AccordionSingle } from '@ecmwf-projects/cads-ui-library'
5
5
6
6
import { GenerateBlocks } from '../GenerateBlocks'
7
7
8
8
import type { AccordionBlock } from '../../models'
9
+ import { useHash } from 'src/hooks/useHash'
9
10
10
11
export const Accordion = ( { block } : { block : AccordionBlock } ) => {
11
12
12
13
13
14
// Get the current hash
14
- const [ hash , setHash ] = useState < string > ( )
15
- useEffect ( ( ) => {
16
- setHash ( window . location . hash )
17
- } , [ ] )
15
+ const hash = useHash ( ) ;
16
+
17
+ console . log ( 'hash' , hash )
18
+ console . log ( 'Block id' , block . id )
18
19
19
20
// Check if the hash is the same as the block id
20
21
const isHash = hash === `#${ block . id } `
@@ -26,6 +27,7 @@ export const Accordion = ({ block }: { block: AccordionBlock }) => {
26
27
}
27
28
return block . collapsed ? '' : block . title
28
29
} , [ isHash , block . title , block . collapsed ] )
30
+ console . log ( 'Block id' , block . id , defaultValue )
29
31
30
32
return ( < AccordionSingle
31
33
rootProps = { {
Original file line number Diff line number Diff line change
1
+ import { useCallback , useEffect , useState } from 'react'
2
+
3
+ export const useHash = ( ) => {
4
+ const [ hash , setHash ] = useState ( ( ) => window . location . hash )
5
+
6
+ const hashChangeHandler = useCallback ( ( ) => {
7
+ setHash ( window . location . hash )
8
+ } , [ ] )
9
+
10
+ useEffect ( ( ) => {
11
+ window . addEventListener ( 'hashchange' , hashChangeHandler )
12
+ return ( ) => {
13
+ window . removeEventListener ( 'hashchange' , hashChangeHandler )
14
+ }
15
+ } , [ ] )
16
+
17
+ return hash
18
+ }
You can’t perform that action at this time.
0 commit comments