Skip to content

Commit

Permalink
Adds theme option to StatusBar
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony0030 committed Oct 24, 2024
1 parent f8df085 commit 03099eb
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
7 changes: 7 additions & 0 deletions packages/@uppy/status-bar/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @uppy/status-bar

## 4.0.4

Released: 2024-10-24
<!-- Included in: Uppy v4.2.1 -->

- @uppy/status-bar: Add theme option (Anthony Veaudry / #5490)

## 4.0.3

Released: 2024-08-20
Expand Down
1 change: 1 addition & 0 deletions packages/@uppy/status-bar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import StatusBar from '@uppy/status-bar'
const uppy = new Uppy()
uppy.use(StatusBar, {
target: 'body',
theme: 'light',
hideUploadButton: false,
showProgressDetails: false,
hideAfterFinish: true,
Expand Down
2 changes: 1 addition & 1 deletion packages/@uppy/status-bar/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@uppy/status-bar",
"description": "A progress bar for Uppy, with many bells and whistles.",
"version": "4.0.3",
"version": "4.0.4",
"license": "MIT",
"main": "lib/index.js",
"style": "dist/style.min.css",
Expand Down
58 changes: 57 additions & 1 deletion packages/@uppy/status-bar/src/StatusBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const defaultOptions = {
showProgressDetails: false,
hideAfterFinish: true,
doneButtonHandler: null,
theme: 'light',
} satisfies StatusBarOptions

/**
Expand All @@ -78,6 +79,8 @@ export default class StatusBar<M extends Meta, B extends Body> extends UIPlugin<
> {
static VERSION = packageJson.version

private darkModeMediaQuery!: MediaQueryList | null

#lastUpdateTime!: ReturnType<typeof performance.now>

#previousUploadedBytes!: number | null
Expand Down Expand Up @@ -231,12 +234,44 @@ export default class StatusBar<M extends Meta, B extends Body> extends UIPlugin<
}

onMount(): void {
// Set the text direction if the page has not defined one.
const { capabilities } = this.uppy.getState()
const element = this.el!

// Set the text direction if the page has not defined one.
const direction = getTextDirection(element)
if (!direction) {
element.dir = 'ltr'
}

// Set the theme
let theme
if (this.opts.theme === 'auto') {
theme = capabilities.darkMode ? 'dark' : 'light'
} else {
theme = this.opts.theme
}

element.dataset.uppyTheme = theme
}

setDarkModeCapability = (isDarkModeOn: boolean): void => {
const { capabilities } = this.uppy.getState()
this.uppy.setState({
capabilities: {
...capabilities,
darkMode: isDarkModeOn,
},
})
}

private handleSystemDarkModeChange = (event: MediaQueryListEvent) => {
const isDarkModeOnNow = event.matches
this.uppy.log(`[Dashboard] Dark mode is ${isDarkModeOnNow ? 'on' : 'off'}`)
this.setDarkModeCapability(isDarkModeOnNow)

const element = this.el!
const theme = isDarkModeOnNow ? 'dark' : 'light'
element.dataset.uppyTheme = theme
}

#onUploadStart = (): void => {
Expand Down Expand Up @@ -267,6 +302,23 @@ export default class StatusBar<M extends Meta, B extends Body> extends UIPlugin<
}
this.uppy.on('upload', this.#onUploadStart)

// Dark Mode / theme
this.darkModeMediaQuery =
typeof window !== 'undefined' && window.matchMedia ?
window.matchMedia('(prefers-color-scheme: dark)')
: null

const isDarkModeOnFromTheStart =
this.darkModeMediaQuery ? this.darkModeMediaQuery.matches : false
this.uppy.log(
`[Dashboard] Dark mode is ${isDarkModeOnFromTheStart ? 'on' : 'off'}`,
)
this.setDarkModeCapability(isDarkModeOnFromTheStart)

if (this.opts.theme === 'auto') {
this.darkModeMediaQuery?.addListener(this.handleSystemDarkModeChange)
}

// To cover the use case where the status bar is installed while the upload
// has started, we set `lastUpdateTime` right away.
this.#lastUpdateTime = performance.now()
Expand All @@ -276,6 +328,10 @@ export default class StatusBar<M extends Meta, B extends Body> extends UIPlugin<
}

uninstall(): void {
if (this.opts.theme === 'auto') {
this.darkModeMediaQuery?.removeListener(this.handleSystemDarkModeChange)
}

this.unmount()
this.uppy.off('upload', this.#onUploadStart)
}
Expand Down
1 change: 1 addition & 0 deletions packages/@uppy/status-bar/src/StatusBarOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export interface StatusBarOptions extends UIPluginOptions {
hideCancelButton?: boolean
doneButtonHandler?: (() => void) | null
locale?: typeof StatusBarLocale
theme?: 'auto' | 'dark' | 'light'
}

0 comments on commit 03099eb

Please sign in to comment.