Skip to content

Commit

Permalink
✨ add breakpoint badge to display current viewport size in developmen…
Browse files Browse the repository at this point in the history
…t mode
  • Loading branch information
AntoineKM committed Jan 16, 2025
1 parent e6c7ab5 commit 2f044d3
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-singers-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"kitchn": minor
---

Add breakpoint badge to display current viewport size in development mode
36 changes: 36 additions & 0 deletions packages/kitchn/src/components/Provider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ import {
UpdateToastsLayoutFunction,
} from "../../contexts/Toasts";
import { withDecorator } from "../../hoc/withDecorator";
import { useBreakpoint } from "../../hooks";
import { useCurrentState } from "../../hooks/useCurrentState";
import { defaultThemes, generateThemes } from "../../themes";
import { Themes } from "../../types";
import { isDevelopment } from "../../utils/isDevelopment";
import Badge from "../Badge";
import Container from "../Container";
import GlobalStyle from "../GlobalStyle";
import Text from "../Text";
import ToastContainer from "../Toast/Container";

export type KitchnProviderProps = {
Expand All @@ -36,6 +41,35 @@ export type KitchnProviderProps = {
themes?: Record<string, DefaultTheme>;
dangerouslyDisableNextThemeProvider?: boolean;
attribute?: NextThemeProviderProps["attribute"];
showBreakpointBadge?: boolean;
};

const BreakpointBadge = ({ show }: { show?: boolean }) => {
const [mounted, setMounted] = React.useState(false);
const { isMobile, isTablet, isLaptop, isDesktop } = useBreakpoint();

// Pour éviter les problèmes d'hydration avec Next.js
React.useEffect(() => {
setMounted(true);
}, []);

if (!mounted || !show || !isDevelopment()) return null;

let breakpoint = "unknown";
if (isMobile) breakpoint = "mobile";
else if (isTablet) breakpoint = "tablet";
else if (isLaptop) breakpoint = "laptop";
else if (isDesktop) breakpoint = "desktop";

return (
<Container position={"fixed"} bottom={"small"} left={"small"} zIndex={1000}>
<Badge type={"primary"}>
<Text size={"inherit"} color={"inherit"} span>
{breakpoint}
</Text>
</Badge>
</Container>
);
};

export const KitchnProviderComponent: React.FC<KitchnProviderProps> = ({
Expand All @@ -46,6 +80,7 @@ export const KitchnProviderComponent: React.FC<KitchnProviderProps> = ({
forcedTheme,
attribute = "data-theme",
dangerouslyDisableNextThemeProvider,
showBreakpointBadge = true,
}: KitchnProviderProps) => {
const staticThemes = { ...defaultThemes, ...customThemes };
const themes = React.useMemo(
Expand Down Expand Up @@ -104,6 +139,7 @@ export const KitchnProviderComponent: React.FC<KitchnProviderProps> = ({
<ToastsContent.Provider value={initialValue}>
{children}
<ToastContainer />
<BreakpointBadge show={showBreakpointBadge} />
</ToastsContent.Provider>
</ThemeProvider>
</StyleSheetManager>
Expand Down
1 change: 1 addition & 0 deletions packages/kitchn/src/utils/_collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export * from "./convertRGBToRGBA";
export * from "./getId";
export * from "./isNumber";
export * from "./isString";
export * from "./isDevelopment";
export * from "./pickChild";
export * from "./shortenName";
9 changes: 9 additions & 0 deletions packages/kitchn/src/utils/isDevelopment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const isDevelopment = (): boolean => {
if (typeof window === "undefined") {
return process.env.NODE_ENV === "development";
}
return (
window.location.hostname === "localhost" ||
window.location.hostname === "127.0.0.1"
);
};

0 comments on commit 2f044d3

Please sign in to comment.