Skip to content

Commit

Permalink
feat(tooltip): add dark mode
Browse files Browse the repository at this point in the history
  • Loading branch information
nlopin committed Aug 28, 2023
1 parent 9ea2bd4 commit 9f8cc2c
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 51 deletions.
25 changes: 13 additions & 12 deletions src/components/ColorMode/InvertedColorMode.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import React, { useEffect, useState } from 'react';
import React, { FC, ReactNode, useEffect, useState } from 'react';

export const InvertedColorMode = ({ children }: { children: React.ReactNode }) => {
const [preferredColorScheme, setPreferredColorScheme] = useState(
window.matchMedia('(prefers-color-scheme: dark)') ? 'light-theme' : 'dark-theme'
export const InvertedColorMode: FC<{ children: ReactNode }> = ({ children }) => {
const [preferredColorSchema, setPreferredColorSchema] = useState(
window.matchMedia('(prefers-color-scheme: dark)').matches ? 'light-theme' : 'dark-theme'
);

useEffect(() => {
const mql = window.matchMedia('(prefers-color-scheme: dark)');
mql.addEventListener('change', e => {
if (e.matches) {
setPreferredColorScheme('light-theme');
} else {
setPreferredColorScheme('dark-theme');
}
});
const onMediaQueryChange = (e: MediaQueryListEvent) => {
setPreferredColorSchema(e.matches ? 'light-theme' : 'dark-theme');
};

mql.addEventListener('change', onMediaQueryChange);
return () => {
mql.removeEventListener('change', onMediaQueryChange);
};
}, []);

return <div className={preferredColorScheme}>{children}</div>;
return <div className={preferredColorSchema}>{children}</div>;
};
4 changes: 0 additions & 4 deletions src/components/Text/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ function determineTextColor(props: TextProps) {
return getSemanticValue(inverted ? 'foreground-neutral-faded' : 'foreground-neutral-emphasized');
}

if (inverted) {
return getSemanticValue('foreground-on-background-primary');
}

return getSemanticValue('foreground-primary');
}

Expand Down
10 changes: 0 additions & 10 deletions src/components/Text/docs/Text.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Meta, StoryObj } from '@storybook/react';
import { Text } from '../Text';
import { onDarkBackground } from '../../../docs/parameters';

const meta: Meta = {
title: 'Components/Text',
Expand Down Expand Up @@ -75,15 +74,6 @@ export const Secondary: Story = {
}
};

export const Inverted: Story = {
args: {
inverted: true
},
parameters: {
...onDarkBackground
}
};

export const Disabled: Story = {
args: {
disabled: true
Expand Down
27 changes: 12 additions & 15 deletions src/components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getSemanticValue } from '../../utils/cssVariables';
import { get } from '../../utils/themeGet';
import { Text } from '../Text/Text';
import { mapPlacementWithDeprecationWarning, TooltipPlacement } from './TooltipPlacement';
import { InvertedColorMode } from '../ColorMode/InvertedColorMode';

const fadeAnimation = keyframes`
from {
Expand Down Expand Up @@ -82,14 +83,13 @@ const arrowPlacementStyles = variant({
});

interface TooltipBodyProps {
inverted?: boolean;
variant: string;
}

const TooltipBody = styled.div<TooltipBodyProps>`
position: relative;
z-index: ${Elevation.TOOLTIP};
background-color: ${p => getSemanticValue(p.inverted ? 'background-surface-neutral-faded' : 'background-backdrop')};
background-color: ${getSemanticValue('background-backdrop')};
padding: 0.25rem 0.5rem;
border-radius: ${get('radii.2')};
opacity: 0;
Expand All @@ -112,9 +112,10 @@ const TooltipBody = styled.div<TooltipBodyProps>`
position: absolute;
pointer-events: none;
border: 0.25rem solid rgba(0, 0, 0, 0);
border-bottom-color: ${p =>
border-bottom-color: ${
// background colors are used because this border is used to create the arrow
getSemanticValue(p.inverted ? 'background-surface-neutral-faded' : 'background-backdrop')};
getSemanticValue('background-backdrop')
};
margin-left: -0.25rem;
${arrowPlacementStyles}
Expand All @@ -123,17 +124,13 @@ const TooltipBody = styled.div<TooltipBodyProps>`

interface TooltipProps {
/**
* The content that will be shown inside of the tooltip body
* The content that will be shown inside the tooltip body
*/
content: React.ReactNode;
/**
* Set the position of where the tooltip is attached to the target, defaults to "top"
*/
placement?: TooltipPlacement | Placement;
/**
* Adjust the component for display on dark backgrounds
*/
inverted?: boolean;
/**
* Force the tooltip to always be visible, regardless of user interaction
*/
Expand All @@ -144,8 +141,7 @@ const Tooltip: React.FC<React.PropsWithChildren<TooltipProps>> = ({
content,
children,
placement = 'top',
alwaysVisible = false,
inverted = false
alwaysVisible = false
}) => {
const [isVisible, setIsVisible] = React.useState(alwaysVisible);
/**
Expand Down Expand Up @@ -178,9 +174,11 @@ const Tooltip: React.FC<React.PropsWithChildren<TooltipProps>> = ({

if (typeof content === 'string') {
dynamicContent = (
<Text as="p" fontSize={0} inverted={!inverted}>
{content}
</Text>
<InvertedColorMode>
<Text as="p" fontSize={0}>
{content}
</Text>
</InvertedColorMode>
);
}

Expand All @@ -202,7 +200,6 @@ const Tooltip: React.FC<React.PropsWithChildren<TooltipProps>> = ({
createPortal(
<TooltipBody
ref={setContentReference}
inverted={inverted}
style={{ ...styles.popper }}
variant={attributes.popper?.['data-popper-placement']}
{...attributes.popper}
Expand Down
6 changes: 0 additions & 6 deletions src/components/Tooltip/docs/Tooltip.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ export const AlwaysVisible: Story = {
}
};

export const Inverted: Story = {
args: {
inverted: true
}
};

export const WithCustomPosition: Story = {
args: {
placement: 'bottom-center'
Expand Down
3 changes: 2 additions & 1 deletion src/essentials/Colors/Colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ export const SemanticColorsDarkSchema = {
default: Colors.orange[900],
emphasized: Colors.orange[1000]
}
}
},
backdrop: Colors.blue.primary[50]
},
border: {
neutral: {
Expand Down
3 changes: 2 additions & 1 deletion src/essentials/Colors/RedesignedColors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ export const SemanticColorsDarkSchema = {
default: Colors.red[900],
emphasized: Colors.red[1000]
}
}
},
backdrop: Colors.neutral[50]
},
border: {
neutral: {
Expand Down
10 changes: 8 additions & 2 deletions src/essentials/Colors/globalStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,25 @@ import { generateBareTierCssVariables, generateSemanticTierCssVariables } from '

export const ClassicColors = createGlobalStyle`
:root {
color-scheme: light dark;
color-scheme: light;
${generateBareTierCssVariables(ClassicBrandColors)}
${generateSemanticTierCssVariables(ClassicSemanticColors)}
}
.dark-theme {
color-scheme: dark;
${generateSemanticTierCssVariables(ClassicSemanticColorsDark)}
}
@media (prefers-color-scheme: dark) {
:root {
color-scheme: dark;
${generateSemanticTierCssVariables(ClassicSemanticColorsDark)}
}
.light-theme {
color-scheme: light;
${generateBareTierCssVariables(ClassicBrandColors)}
${generateSemanticTierCssVariables(ClassicSemanticColors)}
}
Expand All @@ -37,21 +40,24 @@ export const ClassicColors = createGlobalStyle`

export const ModernColors = createGlobalStyle`
:root {
color-scheme: light dark;
color-scheme: light;
${generateBareTierCssVariables(ModernBrandColors)}
${generateSemanticTierCssVariables(ModernSemanticColors)}
}
.dark-theme {
color-scheme: dark;
${generateSemanticTierCssVariables(ModernSemanticColorsDark)}
}
@media (prefers-color-scheme: dark) {
:root {
color-scheme: dark;
${generateSemanticTierCssVariables(ModernSemanticColorsDark)}
}
.light-theme {
color-scheme: light;
${generateBareTierCssVariables(ModernBrandColors)}
${generateSemanticTierCssVariables(ModernSemanticColors)}
}
Expand Down

0 comments on commit 9f8cc2c

Please sign in to comment.