-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Ssr): add SsrPageScript, SsrStateProvider and SsrThemeProvider c…
…omponents (#517) * 🆕 feat(Theme): support for SSR * 🚧 feat: wip * update * 🚧 feat: wip * 🚧 feat: wip * 🚧 feat: wip * complete * nullable return * simplify srr rollup config
- Loading branch information
Showing
17 changed files
with
273 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { defineConfig } from "rollup"; | ||
import { terser } from "rollup-plugin-terser"; | ||
|
||
import typescript from "@rollup/plugin-typescript"; | ||
|
||
export default defineConfig([ | ||
{ | ||
input: "./src/ssr/ssr-state-restore.ts", | ||
output: { | ||
file: "../../../../MASA.Blazor/wwwroot/js/ssr-state-restore.js", | ||
format: "iife", | ||
sourcemap: true, | ||
}, | ||
plugins: [typescript(), terser()], | ||
}, | ||
{ | ||
input: "./src/ssr/page-script.ts", | ||
output: { | ||
file: "../../../../MASA.Blazor/wwwroot/js/ssr-page-script.js", | ||
format: "esm", | ||
sourcemap: true, | ||
}, | ||
plugins: [typescript(), terser()], | ||
}, | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
export type MasaBlazorApplication = { | ||
bar?: number; | ||
top: number; | ||
right: number; | ||
bottom: number; | ||
left: number; | ||
footer?: number; | ||
insetFooter?: number; | ||
}; | ||
|
||
export type MasaBlazorSsrPassiveState = { | ||
application: MasaBlazorApplication; | ||
}; | ||
|
||
export type MasaBlazorSsrState = { | ||
culture?: string; | ||
rtl?: boolean; | ||
dark?: boolean; | ||
passive: MasaBlazorSsrPassiveState; | ||
}; | ||
|
||
export const MASA_BLAZOR_SSR_STATE = "masablazor@ssr-state"; | ||
|
||
export function setTheme(dark: boolean) { | ||
const selector = `.${getThemeCss(!dark)}:not(.theme--independent)` | ||
const elements = document.querySelectorAll(selector); | ||
for (let i = 0; i < elements.length; i++) { | ||
elements[i].classList.remove(getThemeCss(!dark)); | ||
elements[i].classList.add(getThemeCss(dark)); | ||
} | ||
|
||
updateStorage({ dark }); | ||
} | ||
|
||
export function setCulture(culture: string) { | ||
const app = getApp(); | ||
if (!app) return; | ||
|
||
updateStorage({ culture }); | ||
} | ||
|
||
export function setRtl(rtl: boolean, updateCache: boolean = true) { | ||
const app = getApp(); | ||
if (!app) return; | ||
|
||
const rtlCss = "m-application--is-rtl"; | ||
if (!rtl) { | ||
app.classList.remove(rtlCss); | ||
} else if (!app.classList.contains(rtlCss)) { | ||
app.classList.add(rtlCss); | ||
} | ||
|
||
if (updateCache) { | ||
updateStorage({ rtl }); | ||
} | ||
} | ||
|
||
export function updateMain(application: MasaBlazorApplication) { | ||
const main: HTMLElement = document.querySelector(".m-main"); | ||
|
||
const newApplication: MasaBlazorApplication = { | ||
top: application.top ?? getPadding("Top"), | ||
right: application.right ?? getPadding("Right"), | ||
bottom: application.bottom ?? getPadding("Bottom"), | ||
left: application.left ?? getPadding("Left"), | ||
}; | ||
|
||
restoreMain(newApplication); | ||
|
||
function getPadding(prop: "Top" | "Right" | "Bottom" | "Left") { | ||
return Number(main.style[`padding${prop}`].match(/\d+/)[0]); | ||
} | ||
} | ||
|
||
export function updatePassiveState(passive: MasaBlazorSsrPassiveState) { | ||
const oldState = getState() ?? {}; | ||
const state: MasaBlazorSsrState = { | ||
...oldState, | ||
passive, | ||
}; | ||
localStorage.setItem(MASA_BLAZOR_SSR_STATE, JSON.stringify(state)); | ||
} | ||
|
||
export function getThemeCss(dark: boolean) { | ||
return dark ? "theme--dark" : "theme--light"; | ||
} | ||
|
||
function getApp() { | ||
return document.querySelector(".m-application"); | ||
} | ||
|
||
function updateStorage(obj: Partial<MasaBlazorSsrState>) { | ||
const stateStr = localStorage.getItem(MASA_BLAZOR_SSR_STATE); | ||
if (stateStr) { | ||
const state = JSON.parse(stateStr); | ||
localStorage.setItem( | ||
MASA_BLAZOR_SSR_STATE, | ||
JSON.stringify({ | ||
...state, | ||
...obj, | ||
}) | ||
); | ||
} | ||
} | ||
|
||
export function restoreMain(application: MasaBlazorApplication) { | ||
const main = document.querySelector(".m-main") as HTMLElement; | ||
if (main && application) { | ||
main.style.paddingTop = application.top + application.bar + "px"; | ||
main.style.paddingRight = application.right + "px"; | ||
main.style.paddingBottom = | ||
application.bottom + application.insetFooter + application.bottom + "px"; | ||
main.style.paddingLeft = application.left + "px"; | ||
} | ||
} | ||
|
||
export function getState(): MasaBlazorSsrState { | ||
const stateStr = localStorage.getItem(MASA_BLAZOR_SSR_STATE); | ||
if (stateStr) { | ||
return JSON.parse(stateStr); | ||
} | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { getThemeCss, MASA_BLAZOR_SSR_STATE, MasaBlazorSsrState, restoreMain, setRtl } from "./"; | ||
|
||
let firstUpdate = true; | ||
|
||
// Called when the script first gets loaded on the page. | ||
export function onLoad() {} | ||
|
||
// Called when an enhanced page update occurs, plus once immediately after | ||
// the initial load. | ||
export function onUpdate() { | ||
if (firstUpdate) { | ||
firstUpdate = false; | ||
return; | ||
} | ||
|
||
const stateStr = localStorage.getItem(MASA_BLAZOR_SSR_STATE); | ||
if (!stateStr) return; | ||
|
||
const state: MasaBlazorSsrState = JSON.parse(stateStr); | ||
if (!state) return; | ||
|
||
restoreMain(state.passive.application); | ||
restoreTheme(state); | ||
restoreRtl(state); | ||
} | ||
|
||
// Called when an enhanced page update removes the script from the page. | ||
export function onDispose() {} | ||
|
||
export function restoreTheme(state: MasaBlazorSsrState) { | ||
if (typeof state.dark === "boolean") { | ||
const selector = `.${getThemeCss(!state.dark)}:not(.theme--independent)`; | ||
const elements = document.querySelectorAll(selector); | ||
for (let i = 0; i < elements.length; i++) { | ||
elements[i].classList.remove(getThemeCss(!state.dark)); | ||
elements[i].classList.add(getThemeCss(state.dark)); | ||
} | ||
} | ||
} | ||
|
||
export function restoreRtl(state: MasaBlazorSsrState) { | ||
if (typeof state.rtl === "boolean") { | ||
setRtl(state.rtl, false); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/Component/BlazorComponent.Web/src/ssr/ssr-state-restore.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { MASA_BLAZOR_SSR_STATE } from "./"; | ||
import { restoreRtl, restoreTheme } from "./page-script"; | ||
|
||
const stateStr = localStorage.getItem(MASA_BLAZOR_SSR_STATE); | ||
if (stateStr) { | ||
const state = JSON.parse(stateStr); | ||
|
||
restoreTheme(state); | ||
restoreRtl(state); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,4 +87,4 @@ public async Task ToggleAsync() | |
Value = !Value; | ||
await ValueChanged.InvokeAsync(Value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
src/Component/BlazorComponent/wwwroot/js/blazor-component.js.map
Large diffs are not rendered by default.
Oops, something went wrong.