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

Support dark mode #113

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions demo/components_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,14 @@ class Delivery(BaseModel):
],
class_name='border-top mt-3 pt-1',
),
c.Div(
components=[
c.Heading(text='DarkMode', level=2),
c.Markdown(text='`DarkMode` can be used to toggle dark mode on and off.'),
c.DarkMode(),
],
class_name='border-top mt-3 pt-1',
),
c.Div(
components=[
c.Heading(text='Custom', level=2),
Expand Down
1 change: 1 addition & 0 deletions demo/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def api_index() -> list[AnyComponent]:
* `Image` - example [here](/components#image)
* `Iframe` - example [here](/components#iframe)
* `Video` - example [here](/components#video)
* `DarkMode` — example [here](/components#darkmode)
* `Toast` - example [here](/components#toast)
* `Table` — See [cities table](/table/cities) and [users table](/table/users)
* `Pagination` — See the bottom of the [cities table](/table/cities)
Expand Down
1 change: 1 addition & 0 deletions docs/api/python_components.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- Error
- Spinner
- Toast
- DarkMode
- Custom
- Table
- Pagination
Expand Down
48 changes: 48 additions & 0 deletions src/npm-fastui-bootstrap/src/DarkMode.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { FC, useEffect, useState } from 'react'
import { models, useClassName } from 'fastui'

export const DarkMode: FC<models.DarkMode> = (props) => {
const [darkMode, setDarkMode] = useState(() => {
const localData = localStorage.getItem('fastui-dark-mode')
return localData ? JSON.parse(localData) : false
})

useEffect(() => {
localStorage.setItem('fastui-dark-mode', JSON.stringify(darkMode))
document.documentElement.setAttribute('data-bs-theme', darkMode ? 'dark' : 'light')
}, [darkMode])

const handleDarkMode = (darkMode: boolean) => {
document.documentElement.setAttribute('data-bs-theme', darkMode ? 'dark' : 'light')
setDarkMode(darkMode)
}

return (
<span className={useClassName(props)} onClick={() => handleDarkMode(!darkMode)}>
{darkMode ? (
<svg
xmlns="http://www.w3.org/2000/svg"
width="18"
height="18"
fill="currentColor"
className="bi bi-moon-stars-fill"
viewBox="0 0 16 16"
>
<path d="M6 .278a.768.768 0 0 1 .08.858 7.208 7.208 0 0 0-.878 3.46c0 4.021 3.278 7.277 7.318 7.277.527 0 1.04-.055 1.533-.16a.787.787 0 0 1 .81.316.733.733 0 0 1-.031.893A8.349 8.349 0 0 1 8.344 16C3.734 16 0 12.286 0 7.71 0 4.266 2.114 1.312 5.124.06A.752.752 0 0 1 6 .278" />
<path d="M10.794 3.148a.217.217 0 0 1 .412 0l.387 1.162c.173.518.579.924 1.097 1.097l1.162.387a.217.217 0 0 1 0 .412l-1.162.387a1.734 1.734 0 0 0-1.097 1.097l-.387 1.162a.217.217 0 0 1-.412 0l-.387-1.162A1.734 1.734 0 0 0 9.31 6.593l-1.162-.387a.217.217 0 0 1 0-.412l1.162-.387a1.734 1.734 0 0 0 1.097-1.097l.387-1.162zM13.863.099a.145.145 0 0 1 .274 0l.258.774c.115.346.386.617.732.732l.774.258a.145.145 0 0 1 0 .274l-.774.258a1.156 1.156 0 0 0-.732.732l-.258.774a.145.145 0 0 1-.274 0l-.258-.774a1.156 1.156 0 0 0-.732-.732l-.774-.258a.145.145 0 0 1 0-.274l.774-.258c.346-.115.617-.386.732-.732L13.863.1z" />
</svg>
) : (
<svg
xmlns="http://www.w3.org/2000/svg"
width="18"
height="18"
fill="currentColor"
className="bi bi-brightness-high-fill"
viewBox="0 0 16 16"
>
<path d="M12 8a4 4 0 1 1-8 0 4 4 0 0 1 8 0M8 0a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0v-2A.5.5 0 0 1 8 0m0 13a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0v-2A.5.5 0 0 1 8 13m8-5a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1h2a.5.5 0 0 1 .5.5M3 8a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1h2A.5.5 0 0 1 3 8m10.657-5.657a.5.5 0 0 1 0 .707l-1.414 1.415a.5.5 0 1 1-.707-.708l1.414-1.414a.5.5 0 0 1 .707 0m-9.193 9.193a.5.5 0 0 1 0 .707L3.05 13.657a.5.5 0 0 1-.707-.707l1.414-1.414a.5.5 0 0 1 .707 0zm9.193 2.121a.5.5 0 0 1-.707 0l-1.414-1.414a.5.5 0 0 1 .707-.707l1.414 1.414a.5.5 0 0 1 0 .707M4.464 4.465a.5.5 0 0 1-.707 0L2.343 3.05a.5.5 0 1 1 .707-.707l1.414 1.414a.5.5 0 0 1 0 .708z" />
</svg>
)}
</span>
)
}
3 changes: 3 additions & 0 deletions src/npm-fastui-bootstrap/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { ClassNameGenerator, CustomRender, models } from 'fastui'
import { Modal } from './modal'
import { Navbar } from './navbar'
import { Pagination } from './pagination'
import { DarkMode } from './DarkMode'
import { Footer } from './footer'
import { Toast } from './toast'

Expand All @@ -19,6 +20,8 @@ export const customRender: CustomRender = (props) => {
return () => <Modal {...props} />
case 'Pagination':
return () => <Pagination {...props} />
case 'DarkMode':
return () => <DarkMode {...props} />
case 'Toast':
return () => <Toast {...props} />
}
Expand Down
7 changes: 7 additions & 0 deletions src/npm-fastui/src/components/DarkMode.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { FC } from 'react'

import { DarkMode } from '../models'

export const DarkModeComp: FC<DarkMode> = (props: DarkMode) => {
return <>`${props.type} are not implemented by pure FastUI, implement a component for &apos;DarkModeProps&apos;.`</>
}
4 changes: 4 additions & 0 deletions src/npm-fastui/src/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { FireEventComp } from './FireEvent'
import { ErrorComp } from './error'
import { SpinnerComp } from './spinner'
import { CustomComp } from './Custom'
import { DarkModeComp } from './DarkMode'
import { ToastComp } from './toast'

// TODO some better way to export components
Expand All @@ -67,6 +68,7 @@ export {
PaginationComp,
DetailsComp,
DisplayComp,
DarkModeComp,
JsonComp,
FooterComp,
ServerLoadComp,
Expand Down Expand Up @@ -168,6 +170,8 @@ export const AnyComp: FC<FastProps> = (props) => {
return <SpinnerComp {...props} />
case 'Custom':
return <CustomComp {...props} />
case 'DarkMode':
return <DarkModeComp {...props} />
case 'Toast':
return <ToastComp {...props} />
default:
Expand Down
8 changes: 8 additions & 0 deletions src/npm-fastui/src/models.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export type FastProps =
| Error
| Spinner
| Custom
| DarkMode
| Table
| Pagination
| Display
Expand Down Expand Up @@ -339,6 +340,13 @@ export interface Custom {
className?: ClassName
type: 'Custom'
}
/**
* DarkMode component
*/
export interface DarkMode {
className?: ClassName
type: 'DarkMode'
}
/**
* Table component.
*/
Expand Down
9 changes: 9 additions & 0 deletions src/python-fastui/fastui/components/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
'Spinner',
'Toast',
'Custom',
'DarkMode',
# then we include components from other files
'Table',
'Pagination',
Expand Down Expand Up @@ -597,6 +598,13 @@ class Custom(BaseModel, extra='forbid'):
"""The type of the component. Always 'Custom'."""


class DarkMode(BaseModel, extra='forbid'):
"""DarkMode component"""

class_name: _class_name.ClassNameField = None
type: _t.Literal['DarkMode'] = 'DarkMode'


AnyComponent = _te.Annotated[
_t.Union[
Text,
Expand All @@ -622,6 +630,7 @@ class Custom(BaseModel, extra='forbid'):
Error,
Spinner,
Custom,
DarkMode,
Table,
Pagination,
Display,
Expand Down