Skip to content

Commit cb77194

Browse files
Merge pull request #60 from FDMediagroep/develop
v0.4.18
2 parents f37b366 + 9415fd5 commit cb77194

File tree

4 files changed

+30
-28
lines changed

4 files changed

+30
-28
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@fdmg/design-system",
3-
"version": "0.4.17",
3+
"version": "0.4.18",
44
"description": "FD Design System",
55
"types": "main.d.ts",
66
"main": "main.js",

src/components/Tooltip.tsx

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useState, useEffect } from 'react';
22
import styles from './Tooltip.module.scss';
33
import TooltipStore from '../stores/TooltipStore';
44

@@ -17,33 +17,41 @@ export interface Summary {
1717
borderLeftColor?: Token;
1818
}
1919

20-
interface Props {
21-
title: string;
22-
description: Summary;
23-
}
20+
function Tooltip() {
21+
const [tooltipSummary, setTooltipSummary] = useState<Summary>(null);
22+
23+
useEffect(() => {
24+
const tooltipSubscriptionId = TooltipStore.subscribe(() => {
25+
setTooltipSummary(TooltipStore.getSummary());
26+
});
27+
setTooltipSummary(TooltipStore.getSummary());
28+
29+
return () => {
30+
TooltipStore.unsubscribe(tooltipSubscriptionId);
31+
};
32+
}, []);
2433

25-
function Tooltip(props: Props) {
2634
function handleClick() {
2735
TooltipStore.setSummary(null);
2836
}
2937

30-
return (
38+
return tooltipSummary ? (
3139
<div className={styles.tooltip} onClick={handleClick}>
32-
<h3>{props.title}</h3>
40+
<h3>Design Tokens</h3>
3341
{styleProps.map((styleProp) => {
34-
if (props.description && props.description[styleProp]) {
42+
if (tooltipSummary[styleProp]) {
3543
return (
3644
<fieldset key={styleProp}>
3745
<legend>{styleProp}</legend>
38-
<p>{props?.description?.[styleProp]?.name}</p>
39-
<p>{props?.description?.[styleProp]?.rgb}</p>
40-
<p>{props?.description?.[styleProp]?.hex}</p>
46+
<p>{tooltipSummary?.[styleProp]?.name}</p>
47+
<p>{tooltipSummary?.[styleProp]?.rgb}</p>
48+
<p>{tooltipSummary?.[styleProp]?.hex}</p>
4149
</fieldset>
4250
);
4351
}
4452
})}
4553
</div>
46-
);
54+
) : null;
4755
}
4856

4957
const styleProps = [

src/pages/_app.tsx

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ import { Menu } from '../components/Menu';
66
import PageStore, { Page } from '../stores/PageStore';
77
import Head from 'next/head';
88
import { Tooltip } from '../components/Tooltip';
9-
import TooltipStore from '../stores/TooltipStore';
109

1110
function App({ Component, pageProps }) {
12-
const [tooltipSummary, setTooltipSummary] = useState(null);
1311
const [pageType, setPageType] = useState<Page>(PageStore.getPageType());
1412
const [pageStyle, setPageStyle] = useState(styles.overview);
1513
const darkModeMediaQuery =
@@ -28,14 +26,8 @@ function App({ Component, pageProps }) {
2826
});
2927
setPageType(PageStore.getPageType());
3028

31-
const tooltipSubscriptionId = TooltipStore.subscribe(() => {
32-
setTooltipSummary(TooltipStore.getSummary());
33-
});
34-
setTooltipSummary(TooltipStore.getSummary());
35-
3629
return () => {
3730
PageStore.unsubscribe(subscriptionId);
38-
TooltipStore.unsubscribe(tooltipSubscriptionId);
3931
};
4032
}, []);
4133

@@ -65,12 +57,7 @@ function App({ Component, pageProps }) {
6557
<main>
6658
<Component {...pageProps} />
6759
</main>
68-
{tooltipSummary && (
69-
<Tooltip
70-
title="Design Tokens"
71-
description={tooltipSummary}
72-
/>
73-
)}
60+
<Tooltip />
7461
</section>
7562
</>
7663
);

src/utils/designTokensTooltip.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,21 @@ function tooltipHandler(e: MouseEvent) {
7979

8080
const register = [];
8181

82+
function handleMouseOut() {
83+
TooltipStore.setSummary(null);
84+
}
85+
8286
export function clearDesignTokensTooltips() {
8387
register.forEach((el) => {
88+
el.removeEventListener('mouseout', handleMouseOut);
8489
el.removeEventListener('mouseover', tooltipHandler);
8590
});
91+
TooltipStore.setSummary(null);
8692
}
8793

8894
export function initializeDesignTokensTooltip(el: HTMLElement) {
8995
if (window.getComputedStyle) {
96+
el.addEventListener('mouseout', handleMouseOut);
9097
el.addEventListener('mouseover', tooltipHandler);
9198
register.push(el);
9299
}

0 commit comments

Comments
 (0)