Skip to content

Commit fa58536

Browse files
committed
feat: Update new theme support backdropFilter
1 parent 39b57ac commit fa58536

File tree

5 files changed

+73
-31
lines changed

5 files changed

+73
-31
lines changed

docs/src/routes/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class ThemeWrapper extends React.Component {
2323
<Theme
2424
theme={theme}
2525
needGenerateAcrylic={false}
26-
themeWillUpdate={setStaticAcrylicTexture}
26+
// themeWillUpdate={setStaticAcrylicTexture}
2727
>
2828
{children}
2929
</Theme>

src/Theme/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as PropTypes from "prop-types";
33

44
import { handleScrollReveal } from "./handleScrollReveal";
55
import darkTheme from "../styles/darkTheme";
6-
import getTheme, { Theme as ThemeType, ThemeConfig } from "../styles/getTheme";
6+
import getTheme, { Theme as ThemeType } from "../styles/getTheme";
77
import RenderToBody from "../RenderToBody";
88
import ToastWrapper from "../Toast/ToastWrapper";
99
import { StyleManagerSheet } from "./StyleManagerSheet";
@@ -145,7 +145,7 @@ export class Theme extends React.Component<ThemeProps, ThemeState> {
145145
}
146146
} as ThemeType);
147147

148-
if (theme.useFluentDesign && theme.desktopBackgroundImage && theme.acrylicTextureCount !== 3) {
148+
if (theme.useFluentDesign && theme.desktopBackgroundImage) {
149149
theme.generateAcrylicTextures(currTheme => {
150150
this.setState({ currTheme });
151151
this.handleThemeUpdate(currTheme);

src/styles/generateAcrylicTexture.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ export default function generateAcrylicTexture(config: AcrylicTextureConfig) {
1616
const configStr = JSON.stringify({ image, tintColor, tintOpacity, blurSize });
1717
let acrylicTexture = acrylicTextureMap.get(configStr);
1818

19+
function updatAcrylicTexture() {
20+
if (callback) callback(acrylicTexture);
21+
acrylicTextureMap.set(configStr, acrylicTexture);
22+
}
23+
1924
if (acrylicTexture) {
2025
callback(acrylicTexture);
2126
return;
@@ -25,6 +30,7 @@ export default function generateAcrylicTexture(config: AcrylicTextureConfig) {
2530
const context = canvas.getContext("2d");
2631
const imageNode = new Image();
2732
imageNode.crossOrigin = "Anonymous";
33+
imageNode.src = image;
2834

2935
imageNode.onload = () => {
3036
let { naturalWidth, naturalHeight } = imageNode;
@@ -47,18 +53,17 @@ export default function generateAcrylicTexture(config: AcrylicTextureConfig) {
4753
if (HTMLCanvasElement.prototype.toBlob) {
4854
canvas.toBlob((blob) => {
4955
acrylicTexture = URL.createObjectURL(blob);
56+
updatAcrylicTexture();
5057
});
5158
} else if (HTMLCanvasElement.prototype["msToBlob"]) {
5259
const blob = canvas["msToBlob"]();
5360
acrylicTexture = URL.createObjectURL(blob);
61+
updatAcrylicTexture();
5462
} else {
5563
acrylicTexture = canvas.toDataURL("image/jpg");
64+
updatAcrylicTexture();
5665
}
57-
58-
acrylicTextureMap.set(configStr, acrylicTexture);
59-
if (callback) callback(acrylicTexture);
6066
};
61-
6267
}
6368

6469

src/styles/getAcrylicTextureStyle.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
11
import { isSupportBackdropFilter } from "../utils/browser/backdropFilterDetector";
22

3-
export function getAcrylicTextureStyle(): CSSStyleDeclaration {
3+
export { isSupportBackdropFilter };
4+
5+
export interface AcrylicConfig { tintColor: string; opacity: number; blurSize: number; }
6+
7+
export function getAcrylicTextureStyle(config: AcrylicConfig) {
48
const isSupported = isSupportBackdropFilter();
5-
let style: CSSStyleDeclaration;
9+
let style: React.CSSProperties;
10+
const { tintColor, opacity, blurSize } = config;
611
if (isSupported) {
712
style = {
813
/**
914
* Add theme.baseLow color.
1015
*/
11-
backgroundColor: "",
16+
backgroundColor: tintColor,
1217
/**
1318
* Add noise texture.
1419
*/
1520
backgroundImage: "",
1621
/**
1722
* Add blur filter.
1823
*/
19-
backdropFilter: "blur(10px)"
24+
backdropFilter: `blur(${blurSize}px)`
2025
} as any;
2126
} else {
2227
style = {
2328
/**
2429
* Add polyfill texture theme.acrylicText40.
2530
*/
26-
background: ""
27-
} as CSSStyleDeclaration;
31+
background: tintColor
32+
};
2833
}
2934

3035
return style;

src/styles/getTheme.ts

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import prefixAll from "../utils/prefixAll";
33
import { Toast } from "../Toast";
44
import { StyleManager, CustomCSSProperties, StyleClasses } from "./StyleManager";
55
import generateAcrylicTexture from "./generateAcrylicTexture";
6+
import { getAcrylicTextureStyle, AcrylicConfig, isSupportBackdropFilter } from "./getAcrylicTextureStyle";
67

8+
const supportedBackdropFilter = isSupportBackdropFilter();
79
export function darken(color: string, coefficient: number) {
810
const hsl = tinycolor(color).toHsl();
911
hsl.l = hsl.l * (1 - coefficient);
@@ -17,6 +19,7 @@ export function lighten(color: string, coefficient: number) {
1719
}
1820

1921
export interface AcrylicTexture {
22+
style?: React.CSSProperties;
2023
tintColor?: string;
2124
tintOpacity?: number;
2225
blurSize?: number;
@@ -381,39 +384,68 @@ export class Theme {
381384
}
382385
} as Theme);
383386

387+
const blurSize = 24;
388+
const acrylicTexture40Config: AcrylicConfig = {
389+
tintColor: this.chromeMedium,
390+
opacity: .4,
391+
blurSize
392+
};
393+
this.acrylicTexture40.style = getAcrylicTextureStyle(acrylicTexture40Config);
394+
this.acrylicTexture40.background = this.acrylicTexture40.style.background as string;
395+
396+
const acrylicTexture60Config: AcrylicConfig = {
397+
tintColor: this.chromeMediumLow,
398+
opacity: .6,
399+
blurSize
400+
};
401+
this.acrylicTexture60.style = getAcrylicTextureStyle(acrylicTexture60Config);
402+
this.acrylicTexture60.background = this.acrylicTexture60.style.background as string;
403+
404+
const acrylicTexture80Config: AcrylicConfig = {
405+
tintColor: this.chromeLow,
406+
opacity: .8,
407+
blurSize
408+
};
409+
this.acrylicTexture80.style = getAcrylicTextureStyle(acrylicTexture80Config);
410+
this.acrylicTexture80.background = this.acrylicTexture80.style.background as string;
411+
384412
// generateAcrylicTextures method.
385413
this.generateAcrylicTextures = (themeCallback?: (theme?: Theme) => void) => {
414+
if (supportedBackdropFilter) {
415+
return;
416+
}
386417
this.acrylicTextureCount = 0;
387-
const baseConfig = {
388-
blurSize: 24
389-
};
390418

391419
const callback = (image: string, key: number) => {
420+
const background = `url(${image}) no-repeat fixed top left / cover`;
392421
if (key === 4) {
393422
this.acrylicTextureCount += 1;
394423
Object.assign(this.acrylicTexture40, {
395-
tintColor: this.chromeMediumLow,
396-
tintOpacity: 0.4,
397-
background: `url(${image}) no-repeat fixed top left / cover`,
398-
...baseConfig
424+
tintColor: acrylicTexture40Config.tintColor,
425+
tintOpacity: acrylicTexture40Config.opacity,
426+
style: { background },
427+
background,
428+
blurSize
399429
});
400430
}
401431
if (key === 6) {
402432
this.acrylicTextureCount += 1;
403433
Object.assign(this.acrylicTexture60, {
404-
tintColor: this.chromeLow,
405-
tintOpacity: 0.6,
406-
background: `url(${image}) no-repeat fixed top left / cover`,
407-
...baseConfig
434+
tintColor: acrylicTexture60Config.tintColor,
435+
tintOpacity: acrylicTexture60Config.opacity,
436+
style: { background },
437+
background,
438+
blurSize
408439
});
409440
}
410441
if (key === 8) {
411442
this.acrylicTextureCount += 1;
412443
Object.assign(this.acrylicTexture80, {
413-
tintColor: this.chromeLow,
414-
tintOpacity: 0.8,
415-
background: `url(${image}) no-repeat fixed top left / cover`,
416-
...baseConfig
444+
tintColor: acrylicTexture80Config.tintColor,
445+
tintOpacity: acrylicTexture80Config.opacity,
446+
style: { background },
447+
background,
448+
blurSize
417449
});
418450
}
419451

@@ -428,21 +460,21 @@ export class Theme {
428460
image: this.desktopBackgroundImage,
429461
tintColor: this.chromeMediumLow,
430462
tintOpacity: 0.4,
431-
blurSize: baseConfig.blurSize,
463+
blurSize,
432464
callback: image => callback(image, 4)
433465
});
434466
generateAcrylicTexture({
435467
image: this.desktopBackgroundImage,
436468
tintColor: this.chromeLow,
437469
tintOpacity: 0.6,
438-
blurSize: baseConfig.blurSize,
470+
blurSize,
439471
callback: image => callback(image, 6)
440472
});
441473
generateAcrylicTexture({
442474
image: this.desktopBackgroundImage,
443475
tintColor: this.chromeLow,
444476
tintOpacity: 0.8,
445-
blurSize: baseConfig.blurSize,
477+
blurSize,
446478
callback: image => callback(image, 8)
447479
});
448480
};

0 commit comments

Comments
 (0)