MCPcopy
hub / github.com/pmndrs/react-spring / MessageHub

Function MessageHub

demo/src/sandboxes/notification-hub/src/App.tsx:26–82  ·  view source on GitHub ↗
({
  config = { tension: 125, friction: 20, precision: 0.1 },
  timeout = 3000,
  children,
}: MessageHubProps)

Source from the content-addressed store, hash-verified

24}
25
26function MessageHub({
27 config = { tension: 125, friction: 20, precision: 0.1 },
28 timeout = 3000,
29 children,
30}: MessageHubProps) {
31 const refMap = React.useMemo(() => new WeakMap(), [])
32 const cancelMap = React.useMemo(() => new WeakMap(), [])
33 const [items, setItems] = React.useState<Item[]>([])
34
35 const transitions = useTransition(items, {
36 from: { opacity: 0, height: 0, life: '100%' },
37 keys: item => item.key,
38 enter: item => async (next, cancel) => {
39 cancelMap.set(item, cancel)
40 await next({ opacity: 1, height: refMap.get(item).offsetHeight })
41 await next({ life: '0%' })
42 },
43 leave: [{ opacity: 0 }, { height: 0 }],
44 onRest: (result, ctrl, item) => {
45 setItems(state =>
46 state.filter(i => {
47 return i.key !== item.key
48 })
49 )
50 },
51 config: (item, index, phase) => key =>
52 phase === 'enter' && key === 'life' ? { duration: timeout } : config,
53 })
54
55 React.useEffect(() => {
56 children((msg: string) => {
57 setItems(state => [...state, { key: id++, msg }])
58 })
59 }, [children])
60
61 return (
62 <Container>
63 {transitions(({ life, ...style }, item) => (
64 <Message style={style}>
65 <Content ref={(ref: HTMLDivElement) => ref && refMap.set(item, ref)}>
66 <Life style={{ right: life }} />
67 <p>{item.msg}</p>
68 <Button
69 onClick={e => {
70 e.stopPropagation()
71 if (cancelMap.has(item) && life.get() !== '0%')
72 cancelMap.get(item)()
73 }}
74 >
75 <X size={18} />
76 </Button>
77 </Content>
78 </Message>
79 ))}
80 </Container>
81 )
82}
83

Callers

nothing calls this directly

Calls 3

useTransitionFunction · 0.85
setMethod · 0.65
getMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…