Skip to content

Commit 61ebd24

Browse files
committed
Add re-attach to playground
1 parent f7d8b89 commit 61ebd24

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

web/packages/hovercards/playground/index.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@
3333
color: blue;
3434
cursor: pointer;
3535
}
36+
37+
.gravatar-hovercard-attacher > div {
38+
margin-top: 10px;
39+
}
40+
41+
.gravatar-hovercard-attacher input {
42+
margin-right: 10px;
43+
}
3644
</style>
3745
<body>
3846
<main id="main">

web/packages/hovercards/playground/react.tsx

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable import/no-unresolved */
22

33
import { createRoot } from 'react-dom/client';
4-
import React, { useRef, useEffect } from 'react';
4+
import React, { useRef, useEffect, useState } from 'react';
55

66
import type { HovercardsProps } from '../dist/index.react.d';
77
import { useHovercards, Hovercards } from '../dist/index.react';
@@ -20,37 +20,55 @@ function App() {
2020
// eslint-disable-next-line no-console
2121
const { attach } = useHovercards( { onFetchProfileSuccess: ( hash ) => console.log( hash ) } );
2222
const containerRef = useRef( null );
23+
const attacherRef = useRef( null );
24+
const [ hashes, setHashes ] = useState( [
25+
'33252cd1f33526af53580fcb1736172f06e6716f32afdd1be19ec3096d15dea5',
26+
'c3bb8d897bb538896708195dd9eb162f585654611c50a3a1c9a16a7b64f33270',
27+
'20e74a1399c883caeeba81b57007bcaa058940dcdffca01babfddbaefa5c3c4a',
28+
] );
2329

2430
useEffect( () => {
2531
if ( containerRef.current ) {
2632
attach( containerRef.current );
2733
}
2834
}, [ attach ] );
2935

36+
function recycle() {
37+
const newHashes = [ ...hashes ];
38+
39+
for ( let i = newHashes.length - 1; i > 0; i-- ) {
40+
const j = Math.floor( Math.random() * ( i + 1 ) );
41+
[ newHashes[ i ], newHashes[ j ] ] = [ newHashes[ j ], newHashes[ i ] ];
42+
}
43+
44+
setHashes( newHashes );
45+
46+
if ( attacherRef.current ) {
47+
attach( attacherRef.current );
48+
}
49+
}
50+
3051
return (
3152
<div style={ { display: 'flex', flexDirection: 'column', alignItems: 'center', gap: '5rem' } }>
3253
<div>
3354
<div ref={ containerRef } style={ { display: 'flex', flexDirection: 'column', gap: '5rem' } }>
34-
<img
35-
src="https://www.gravatar.com/avatar/33252cd1f33526af53580fcb1736172f06e6716f32afdd1be19ec3096d15dea5?s=60&d=retro&r=g"
36-
width="60"
37-
height="60"
38-
alt="Gravatar"
39-
/>
40-
<img
41-
src="https://www.gravatar.com/avatar/c3bb8d897bb538896708195dd9eb162f585654611c50a3a1c9a16a7b64f33270"
42-
width="60"
43-
height="60"
44-
alt="Gravatar"
45-
/>
55+
{ hashes.map( ( hash ) => (
56+
<img
57+
key={ hash }
58+
src={ `https://gravatar.com/avatar/${ hash }?s=128&d=retro&r=g` }
59+
width="60"
60+
height="60"
61+
alt="Gravatar"
62+
/>
63+
) ) }
4664
</div>
4765
</div>
4866
<Hovercards
4967
style={ { display: 'flex', flexDirection: 'column', alignItems: 'center', gap: '5rem' } }
5068
{ ...props }
5169
>
5270
<img
53-
src="https://www.gravatar.com/avatar/20e74a1399c883caeeba81b57007bcaa058940dcdffca01babfddbaefa5c3c4a?s=60&d=retro&r=g"
71+
src="https://gravatar.com/avatar/20e74a1399c883caeeba81b57007bcaa058940dcdffca01babfddbaefa5c3c4a?s=60&d=retro&r=g"
5472
width="60"
5573
height="60"
5674
alt="Gravatar"
@@ -62,6 +80,12 @@ function App() {
6280
@WellyTest
6381
</div>
6482
</Hovercards>
83+
84+
<div className="gravatar-hovercard-attacher">
85+
<button type="button" onClick={ recycle }>
86+
Recycle
87+
</button>
88+
</div>
6589
</div>
6690
);
6791
}

0 commit comments

Comments
 (0)