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

Instead of checking for size and variant every time, they are set as … #545

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -7,16 +7,29 @@ type CopyButtonProps = ButtonProps & {
text?: string;
};

const COPY_ICON_SIZES = {
const COPY_ICON_SIZES: Record<string, number> = {
'1': 15,
'2': 19,
'3': 24,
'4': 29,
};

const CopyButton = ({ value, text = 'Copy', ...props }: CopyButtonProps) => {
const size = props.size || '1';
const variant = props.variant || 'soft';
const CopyButton = ({
value,
text = 'Copy',
size = '1',
variant = 'soft',
...props
}: CopyButtonProps) => {
const iconSize = COPY_ICON_SIZES[size];

const handleCopy = async () => {
try {
await navigator.clipboard.writeText(value);
} catch (err) {
console.error('Failed to copy:', err);
}
};

return (
<Button
Expand All @@ -25,16 +38,14 @@ const CopyButton = ({ value, text = 'Copy', ...props }: CopyButtonProps) => {
variant={variant}
size={size}
color="gray"
iconSize={COPY_ICON_SIZES[size as string]}
rightIcon={IconCopy}
iconSize={iconSize}
iconColor="text-muted"
onClick={async () => {
await navigator.clipboard.writeText(value);
}}
onClick={handleCopy}
>
{text}
</Button>
);
};

export default CopyButton;
export default CopyButton;
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,21 @@ export type JsonViewerProps = BaseProps<{

export function JsonViewer({ data, className, ...props }: JsonViewerProps) {
const classes = styles();
const [style, setStyle] = useState(defaultStyles);
const { theme } = useTheme();
const [style, setStyle] = useState(defaultStyles);

// TODO: theme is loaded as dark but doesn't render in JsonView correctly
// using this solution we force the state to change
// and rerender the component
useEffect(() => {
setStyle(theme === 'dark' ? darkStyles : defaultStyles);
}, [theme]);

return (
<JsonView
data={data}
shouldExpandNode={collapseAllNested}
style={
{
...style,
container: cx(classes.json(), className),
} as any
}
style={{
...style,
container: cx(classes.json(), className),
}}
{...props}
/>
);
Expand All @@ -46,4 +42,4 @@ const styles = tv({
slots: {
json: 'bg-transparent text-sm py-2 px-1 break-all',
},
});
});