Skip to content

Commit

Permalink
Merge pull request #22 from danskernesdigitalebibliotek/with-visibility
Browse files Browse the repository at this point in the history
With visibility
  • Loading branch information
spaceo authored Oct 31, 2024
2 parents b82b3a3 + 252cbe3 commit 9d2617f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default function RootLayout({
return (
<html lang="en">
<body className={`${GTFlexa.variable} antialiased`}>
{getEnvironment() === "development" && <GridHelper />}
<GridHelper hideInProduction />
<Theme>
<ReactQueryProvider>
<Header />
Expand Down
19 changes: 12 additions & 7 deletions components/global/gridHelper/GridHelper.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
'use client'
import { useEffect, useState } from 'react'
"use client"

import { useEffect, useState } from "react"

import withVisibility from "@/lib/helpers/visibility"

// This component is used to show a grid overlay on the screen when the user presses cmd + g
export default function GridHelper() {
function GridHelper() {
const [isShowing, setIsShowing] = useState(false)

useEffect(() => {
window.addEventListener('keydown', handleKeydown)
window.addEventListener("keydown", handleKeydown)
return () => {
window.removeEventListener('keydown', handleKeydown)
window.removeEventListener("keydown", handleKeydown)
}
}, [])

const handleKeydown = (event: KeyboardEvent) => {
if (event.metaKey && event.key === 'g') {
if (event.metaKey && event.key === "k") {
setIsShowing(currentValue => !currentValue)
}
}
Expand All @@ -23,7 +26,7 @@ export default function GridHelper() {
return (
<>
{isShowing ? (
<div className="content-container bg-transparent pointer-events-none fixed inset-0 z-[1000] mx-auto">
<div className="content-container pointer-events-none fixed inset-0 z-[1000] mx-auto bg-transparent">
<div className="grid-go h-full">
{Array.from({ length: columns }).map((e, index) => (
<div
Expand All @@ -37,3 +40,5 @@ export default function GridHelper() {
</>
)
}

export default withVisibility(GridHelper)
18 changes: 18 additions & 0 deletions lib/helpers/visibility.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from "react"

import { getEnvironment } from "@/lib/helpers/helper.env"

interface WithVisibilityProps {
hideInProduction?: boolean
}

export default function withVisibility<P extends object>(WrappedComponent: React.ComponentType<P>) {
const ComponentWithVisibility = ({ hideInProduction, ...props }: P & WithVisibilityProps) => {
if (getEnvironment() === "production" && hideInProduction) {
return null
}
return <WrappedComponent {...(props as P)} />
}

return ComponentWithVisibility
}

0 comments on commit 9d2617f

Please sign in to comment.