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

Feature Request: Configurable Browser Globals #167

Open
huntabyte opened this issue Dec 5, 2024 · 0 comments · May be fixed by #186
Open

Feature Request: Configurable Browser Globals #167

huntabyte opened this issue Dec 5, 2024 · 0 comments · May be fixed by #186
Labels
enhancement New feature or request

Comments

@huntabyte
Copy link
Member

Describe the feature in detail (code, mocks, or screenshots encouraged)

Add the ability to configure custom window, document, navigator, and location objects for utilities that currently rely on global objects. This would make the library more flexible for testing environments, iframes, and other non-standard contexts.

Current Problem

Currently, utilities in the library directly reference global objects like window and document, creating a few challenges:

  • Testing becomes difficult as we can't easily mock global objects
  • Working with iframes requires awkward workarounds
  • Shadow DOM scenarios (surprisingly Shadow DOM support requests have been on the rise in all libraries I maintain)

Proposed API

This is how it's handled in VueUse, and it makes a lot of sense. Anywhere we use a global, we provide an optional config where the user can provide their own.

// configurable-globals.ts
import { BROWSER } from 'esm-env';

export type ConfigurableWindow = {
  window?: Window
}

export type ConfigurableDocument = {
  document?: Document
}

export type ConfigurableNavigator =  {
  navigator?: Navigator
}

export type ConfigurableLocation = {
  location?: Location
}

// Default values
export const defaultWindow = /* #__PURE__ */ BROWSER ? window : undefined
export const defaultDocument = /* #__PURE__ */ BROWSER ? window.document : undefined
export const defaultNavigator = /* #__PURE__ */ BROWSER ? window.navigator : undefined
export const defaultLocation = /* #__PURE__ */ BROWSER ? window.location : undefined

What type of pull request would this be?

None

Provide relevant links or additional information.

No response

@huntabyte huntabyte added the enhancement New feature or request label Dec 5, 2024
@huntabyte huntabyte linked a pull request Dec 20, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant