-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
138 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,39 @@ | ||
import React from 'react'; | ||
import { Button as SpectrumButton, SpectrumButtonProps } from '@react-spectrum/button'; | ||
import './styles.scss'; | ||
|
||
export const Button: React.FC<SpectrumButtonProps> = (props: SpectrumButtonProps) => { | ||
const defaultProps = { | ||
...props, | ||
variant: props.variant ?? 'accent', | ||
style: props.style ?? 'fill' | ||
} | ||
return <SpectrumButton {...defaultProps} UNSAFE_className="fds-button"/>; | ||
import React from "react"; | ||
import { | ||
Button as SpectrumButton, | ||
SpectrumButtonProps, | ||
} from "@react-spectrum/button"; | ||
import "./styles.scss"; | ||
|
||
// Check if children is an icon-only button | ||
function isIconOnly(children: React.ReactNode): boolean { | ||
// Check if it's undefined, null, or an empty string | ||
if (!children || (typeof children === "string" && children.trim() === "")) { | ||
return true; | ||
} | ||
|
||
// Check if children is a single React element without text content | ||
if (React.isValidElement(children)) { | ||
const textContent = React.Children.toArray(children).filter( | ||
(child) => typeof child === "string" && child.trim() !== "" | ||
); | ||
return textContent.length === 0; // No text, likely an icon | ||
} | ||
|
||
return false; | ||
} | ||
|
||
export const Button: React.FC<SpectrumButtonProps> = ( | ||
props: SpectrumButtonProps | ||
) => { | ||
// Check if it's icon-only and conditionally add the class | ||
const className = isIconOnly(props.children) ? "fds-icon-only-button" : "fds-button"; | ||
|
||
const defaultProps = { | ||
...props, | ||
variant: props.variant ?? "accent", | ||
style: props.style ?? "fill", | ||
}; | ||
|
||
return <SpectrumButton {...defaultProps} UNSAFE_className={className} />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters