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

fix: adjust isDom detection #694

Merged
merged 2 commits into from
Jun 23, 2023
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
7 changes: 7 additions & 0 deletions .changeset/light-beds-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@zag-js/dom-query": patch
"@zag-js/focus-visible": patch
"@zag-js/number-utils": patch
---

Improve DOM detection code
2 changes: 1 addition & 1 deletion packages/utilities/dom-query/src/platform.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const isDom = () => typeof window !== "undefined"
export const isDom = () => typeof document !== "undefined"

export function getPlatform() {
const agent = (navigator as any).userAgentData
Expand Down
3 changes: 3 additions & 0 deletions packages/utilities/focus-visible/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
},
"clean-package": "../../../clean-package.config.json",
"main": "src/index.ts",
"dependencies": {
"@zag-js/dom-query": "workspace:*"
},
"devDependencies": {
"clean-package": "2.2.0"
}
Expand Down
4 changes: 3 additions & 1 deletion packages/utilities/focus-visible/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isDom } from "@zag-js/dom-query"

type Modality = "keyboard" | "pointer" | "virtual"
type HandlerEvent = PointerEvent | MouseEvent | KeyboardEvent | FocusEvent
type Handler = (modality: Modality, e: HandlerEvent | null) => void
Expand Down Expand Up @@ -96,7 +98,7 @@ function isFocusVisible() {
}

function setupGlobalFocusEvents() {
if (typeof window === "undefined" || hasSetup) {
if (!isDom() || hasSetup) {
return
}

Expand Down
2 changes: 1 addition & 1 deletion packages/utilities/number/src/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function round(v: number | string, t?: number) {
}

export function roundToDevicePixel(num: number) {
if (typeof window === "undefined") return Math.round(num)
if (typeof window.devicePixelRatio !== "number") return Math.round(num)
const dp = window.devicePixelRatio
return Math.floor(num * dp + 0.5) / dp
}
Expand Down
4 changes: 4 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.