Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Label info link + update Tooltip theme setting #242

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,19 @@ export const inputPreview: ComponentPreview = {
},
],
},
{
type: "select",
prop: "infoLink",
options: [
{
value: "",
label: "No info link",
},
{
value: "https://stellar.org",
label: "Info link",
},
],
},
],
};
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,19 @@ export const selectPreview: ComponentPreview = {
},
],
},
{
type: "select",
prop: "infoLink",
options: [
{
value: "",
label: "No info link",
},
{
value: "https://stellar.org",
label: "Info link",
},
],
},
],
};
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,19 @@ export const textareaPreview: ComponentPreview = {
},
],
},
{
type: "select",
prop: "infoLink",
options: [
{
value: "",
label: "No info link",
},
{
value: "https://stellar.org",
label: "Info link",
},
],
},
],
};
8 changes: 7 additions & 1 deletion @stellar/design-system/src/components/Floater/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ interface FloaterProps {
hasActiveInsideClick?: boolean;
isContrast?: boolean;
showArrow?: boolean;
callback?: (isOpen: boolean) => void;
}

const getArrowStyle = ({
Expand Down Expand Up @@ -118,6 +119,7 @@ export const Floater: React.FC<FloaterProps> = ({
hasActiveInsideClick,
isContrast = true,
showArrow = false,
callback,
}: FloaterProps) => {
const parentRef = useRef<HTMLDivElement | null>(null);
const floaterRef = useRef<HTMLDivElement | null>(null);
Expand Down Expand Up @@ -197,8 +199,12 @@ export const Floater: React.FC<FloaterProps> = ({
} else {
closeFloater();
}

if (callback) {
callback(isOpen);
}
},
[openFloater, closeFloater],
[callback, openFloater, closeFloater],
);

const handleClickOutside = useCallback(
Expand Down
8 changes: 4 additions & 4 deletions @stellar/design-system/src/components/Input/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export interface InputProps {
copyButton?: InputCopyButton;
/** Info text tooltip */
infoText?: string | React.ReactNode;
/** Custom info text icon @defaultValue `<Icon.InfoCircle />` */
infoTextIcon?: React.ReactNode;
/** External link to open in new window */
infoLink?: string;
}

/** */
Expand Down Expand Up @@ -75,7 +75,7 @@ export const Input: React.FC<Props> = ({
success,
copyButton,
infoText,
infoTextIcon,
infoLink,
...props
}: Props) => {
const [isPasswordMasked, setIsPasswordMasked] = useState(true);
Expand Down Expand Up @@ -108,7 +108,7 @@ export const Input: React.FC<Props> = ({
size={fieldSize}
labelSuffix={labelSuffix}
infoText={infoText}
infoTextIcon={infoTextIcon}
infoLink={infoLink}
>
{label}
</Label>
Expand Down
17 changes: 14 additions & 3 deletions @stellar/design-system/src/components/Label/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface LabelProps extends React.LabelHTMLAttributes<HTMLLabelElement> {
isUppercase?: boolean;
labelSuffix?: string | React.ReactNode;
infoText?: string | React.ReactNode;
infoTextIcon?: React.ReactNode;
infoLink?: string;
}

export const Label: React.FC<LabelProps> = ({
Expand All @@ -20,7 +20,7 @@ export const Label: React.FC<LabelProps> = ({
isUppercase,
labelSuffix,
infoText,
infoTextIcon = <Icon.InfoCircle />,
infoLink,
...props
}: LabelProps) => {
const additionalClasses = [
Expand All @@ -41,11 +41,22 @@ export const Label: React.FC<LabelProps> = ({
) : null}
</label>

{infoLink ? (
<a
href={infoLink}
className="Label__infoButton"
rel="noreferrer noopener"
target="_blank"
>
<Icon.BookOpen01 />
</a>
) : null}

{infoText ? (
<Tooltip
triggerEl={
<div className="Label__infoButton" role="button">
{infoTextIcon}
<Icon.InfoCircle />
</div>
}
>
Expand Down
8 changes: 4 additions & 4 deletions @stellar/design-system/src/components/Select/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export interface SelectProps {
success?: string | React.ReactNode;
/** Info text tooltip */
infoText?: string | React.ReactNode;
/** Custom info text icon @defaultValue `<Icon.InfoCircle />` */
infoTextIcon?: React.ReactNode;
/** External link to open in new window */
infoLink?: string;
/** Make label uppercase */
isLabelUppercase?: boolean;
/** Select error without a message */
Expand Down Expand Up @@ -55,7 +55,7 @@ export const Select: React.FC<Props> = ({
error,
success,
infoText,
infoTextIcon,
infoLink,
isLabelUppercase,
isError,
customSelect,
Expand All @@ -81,7 +81,7 @@ export const Select: React.FC<Props> = ({
size={fieldSize}
labelSuffix={labelSuffix}
infoText={infoText}
infoTextIcon={infoTextIcon}
infoLink={infoLink}
>
{label}
</Label>
Expand Down
8 changes: 4 additions & 4 deletions @stellar/design-system/src/components/Textarea/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export interface TextareaProps {
success?: string | React.ReactNode;
/** Info text tooltip */
infoText?: string | React.ReactNode;
/** Custom info text icon @defaultValue `<Icon.InfoCircle />` */
infoTextIcon?: React.ReactNode;
/** External link to open in new window */
infoLink?: string;
/** Textarea error without a message */
isError?: boolean;
/** Make label uppercase */
Expand Down Expand Up @@ -57,7 +57,7 @@ export const Textarea: React.FC<Props> = ({
error,
success,
infoText,
infoTextIcon,
infoLink,
isError,
isLabelUppercase,
hasCopyButton,
Expand Down Expand Up @@ -88,7 +88,7 @@ export const Textarea: React.FC<Props> = ({
size={fieldSize}
labelSuffix={labelSuffix}
infoText={infoText}
infoTextIcon={infoTextIcon}
infoLink={infoLink}
>
{label}
</Label>
Expand Down
12 changes: 2 additions & 10 deletions @stellar/design-system/src/components/ThemeSwitch/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,14 @@ export const ThemeSwitch = ({
if (isDarkMode) {
document.body.classList.remove(ThemeMode.LIGHT);
document.body.classList.add(ThemeMode.DARK);
document.body.setAttribute("data-sds-theme", ThemeMode.DARK);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other components can check for this attribute to get the current theme.

} else {
document.body.classList.remove(ThemeMode.DARK);
document.body.classList.add(ThemeMode.LIGHT);
document.body.setAttribute("data-sds-theme", ThemeMode.LIGHT);
}
}, [disableSetThemeOnLoad, isDarkMode]);

// Trigger custom event to get the current theme
useEffect(() => {
document.dispatchEvent(
new CustomEvent<{ theme: string }>("SDS_ThemeSwitchEvent", {
bubbles: true,
detail: { theme: isDarkMode ? ThemeMode.DARK : ThemeMode.LIGHT },
}),
);
}, [isDarkMode]);

const handleSwitch = () => {
const _isDarkMode = !isDarkMode;

Expand Down
21 changes: 10 additions & 11 deletions @stellar/design-system/src/components/Tooltip/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from "react";
import { useState } from "react";
import { Floater } from "../Floater";
import "./styles.scss";

Expand Down Expand Up @@ -43,16 +43,6 @@ export const Tooltip: React.FC<TooltipProps> = ({
}: TooltipProps) => {
const [theme, setTheme] = useState("sds-theme-dark");

useEffect(() => {
document.addEventListener("SDS_ThemeSwitchEvent", ((
event: CustomEvent<{ theme: string }>,
) => {
if (event.detail?.theme) {
setTheme(event.detail.theme);
}
}) as EventListener);
}, []);

const getContrastMode = () =>
theme === "sds-theme-dark" ? "sds-theme-light" : "sds-theme-dark";

Expand All @@ -63,6 +53,15 @@ export const Tooltip: React.FC<TooltipProps> = ({
isVisible={isVisible}
isContrast={isContrast}
showArrow={true}
callback={(isOpen) => {
if (isOpen) {
const theme = document.body.getAttribute("data-sds-theme");

if (theme) {
setTheme(theme);
}
}
}}
>
Comment on lines +56 to +64
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a callback is more accurate than using a listener.

<div className={`Tooltip ${isContrast ? getContrastMode() : ""}`}>
{title ? <div className="Tooltip__title">{title}</div> : null}
Expand Down
Loading