-
-
Notifications
You must be signed in to change notification settings - Fork 26
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 suggestion auto/default theme #13
Comments
This is a good idea! I've been meaning to go back an tweak the themes a bit so Light/Dark could be toggled from the frontend. My thinking is, like you've put above, we have a new If another theme, like Terminal, or any others that may get added, are chosen, the toggler isn't displayed. Or, if we have a Light/Dark pair - so Light/Dark, TerminalLight/TerminalDark, etc - we could do something like: # default
Use-PodeWebTemplates -Title Tools -Theme Default -ThemeStyle Dark
Use-PodeWebTemplates -Title Tools -Theme Default -ThemeStyle Light
Use-PodeWebTemplates -Title Tools -Theme Default -ThemeStyle Auto
# terminal
Use-PodeWebTemplates -Title Tools -Theme Terminal -ThemeStyle Dark
Use-PodeWebTemplates -Title Tools -Theme Terminal -ThemeStyle Light
Use-PodeWebTemplates -Title Tools -Theme Terminal -ThemeStyle Auto that could be interesting 🤔 |
I was thinking of implementing something like this by myself. I don't know if this is something to include built into the framework or something to leave for the developer to set up and provide an example of that can be copied and customized. I don't understated yet how the session information is saved in Pode. for example in the tool I created if I login with a user, then stop and start the server and refresh the page I get logged out. why is that? |
The session cookie is purely the current session's ID, it can be a cookie or a header. The session data itself is stored within Pode's default in-memory session storage, and when the server is restarted this storage is wiped. Each request from the client is sent to Pode server with the session cookie, that sessionId is verified, and if it doesn't exist in the storage then a 401 is returned (hence logging out after a restart). This is handy for simple setups where no data is ever needed, but if session data should persist a custom User config should be stored where ever your users' details are stored, that way it persists through sessions. So if it was some AD, that could be Name, DOB, Department, etc. For something like the theme, obviously AD doesn't have anything for that. Normally something like this would be stored as a separate cookie, say I do want to simplify the way the themes get set, something like It would also be nice if Pode.Web has internal support to check for a |
I played with this today to understand how it works, here what I came up with ili101/PWT@3973c90 (rough concept 😬)
There are 2 things I didn't manage to do: |
Added editing your user configs from DB |
@ili101 Whoa, very cool! The theme gets changed for everyone, as Another idea could be to have a Actually, both would be neat: the cookie for people who are using an auth like AD and the theme is cookie based/cached, and the property for people who have a custom store and can store the theme for a user 🤔 As for refreshing the page: at the moment, not possible, but it's a simple |
Interesting. So in the example I made if I use AD authentication and I have the SQLite with all the user configuration including the theme. |
With the inbuilt Windows AD, the user object won't have anything like a "theme" property. You could of course use an AD, and then store extra settings in some SQLite DB; using some middleware to get this extra config using the user's email for example. So it would go:
$mware = {
# get the config for the user
$config = Get-UserConfigFromSql -Email $WebEvent.Auth.User.Email
# add theme prop
$WebEvent.Auth.User.Theme = $config.Theme
# or add all config:
$WebEvent.Auth.User.CustomConfig = $config
}
Add-PodeRoute -Middleware $mware -Path etc, etc You would need to set that middleware on each route, because it would need to route after auth. This would only apply to any inbuilt auth that gets the user object directly - such as Windows AD, File, and IIS. Raw basic, form, and even Azure AD you won't need an extra middleware as these are all piped into If the theme was stored at say Set-PodeWebLoginPage -Authentication Example -ThemeProperty Theme
|
Ok I think I understated now 🤔 |
As I was writing that post yesterday I was also thinking the same thing! 😂 I'll see if it's feasible and write it up, adding an optional |
In the above commit, I've rejigged the theming logic a bit. If you set a |
Awesome! and rats! I knew I forgot to add something: added a new |
My pilot project is in mostly usable state 😀 all the major things are working 🙏
|
|
|
Is it finished or partial commit? with 2b7d28b I always get dark Import-Module -Name '\Pode\src\Pode.psd1', '\Pode.Web\src\Pode.Web.psd1' -Force
Start-PodeServer {
Add-PodeEndpoint -Address localhost -Protocol Http
Use-PodeWebTemplates -Title 'Example' -Theme Auto
Set-PodeWebHomePage -Title 'T'
}
Think I found it
need to have "matches" var isSystemDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
Also once cookie is set it's there forever (In case Windows theme setting is changed)? I tried to make a reset button but I think Start-PodeServer {
Add-PodeEndpoint -Address localhost -Protocol Http
Use-PodeWebTemplates -Title 'Example' -Theme Auto
Add-PodeWebPage -Name kk -ScriptBlock {
New-PodeWebCard -Name 'Search' -Content @(
New-PodeWebText -Value (Get-PodeCookie -Name 'pode.web.theme' | Out-String)
New-PodeWebButton -Name 'Remove' -ScriptBlock {
Remove-PodeCookie -Name 'pode.web.theme'
}
)
}
Add-PodeRoute -Method Get -Path '/remove' -ScriptBlock {
Remove-PodeCookie -Name 'pode.web.theme'
}
} |
Yep, The cookie at the moment is set to expire after 30 days - normally I wouldn't expect people to flip from light/dark system themes very often, if at all. I could drop to 7 days, or make it so the cookie auto-expires when the browser is closed? The An wow, I can't believe I missed off |
I tried to understand how it works yesterday. I made the JS update the cookie every time and added an extra test in the PS code to use the cookie or ignore it. so it's updated on every page load, but then I can't know if I need to refresh the page from the JS or not so it can't refresh automatically to show the theme ili101@89c464f. Anyway leave it as is 30 days is good I think. |
Aaah, you gave me an idea. That commit is a little cleverer on detecting system theme switches - and keeps in memory if the theme was Auto. If auto, get system and body theme. If they match, do nothing, if not, update cookie and refresh. |
It have similar problem to what I got yesterday, if there is an Auth Theme override that is different from the system theme it gets in infinite refresh loop, this is why in ili101@89c464f I put it in |
Another way could be to change the priorities when finding the theme. Instead of Auth>Cookie>Server, make it Cookie>Auth>Server 🤔 function Get-PodeWebTheme
{
[CmdletBinding()]
param()
$theme = Get-PodeWebCookie -Name 'theme'
if (($null -ne $theme) -and ![string]::IsNullOrWhiteSpace($theme.Value)) {
return $theme.Value.ToLowerInvariant()
}
$theme = Get-PodeWebAuthTheme -AuthData (Get-PodeWebAuthData)
if (![string]::IsNullOrWhiteSpace($theme)) {
return $theme.ToLowerInvariant()
}
return (Get-PodeWebState -Name 'theme').ToLowerInvariant()
} |
But then Auth will never be applied as Cookie will be there. |
Implemented your idea of using target theme instead, seems like it should cover most use cases. Only change I made was to add an |
Nice. that's cleaner 😀 |
On Pode, you mean the ScriptBlock/auth ticket? Gonna do some last checks on it and merge later hopefully. |
Tested. all good in this issue. |
Use-PodeWebTemplates -Title Tools -Theme Auto
https://css-tricks.com/a-complete-guide-to-dark-mode-on-the-web/
The text was updated successfully, but these errors were encountered: