-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(framework): Make addCustomCSS dynamic (#2083)
- Loading branch information
1 parent
974401b
commit 7b54b9b
Showing
9 changed files
with
135 additions
and
34 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
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 |
---|---|---|
@@ -1,19 +1,40 @@ | ||
const customCSSFor = {}; | ||
import RenderScheduler from "../RenderScheduler.js"; | ||
import EventProvider from "../EventProvider.js"; | ||
|
||
const addCustomCSS = (tag, css, ...rest) => { | ||
if (rest.length) { | ||
throw new Error("addCustomCSS no longer accepts theme specific CSS. new signature is `addCustomCSS(tag, css)`"); | ||
} | ||
const eventProvider = new EventProvider(); | ||
const CUSTOM_CSS_CHANGE = "CustomCSSChange"; | ||
|
||
const attachCustomCSSChange = listener => { | ||
eventProvider.attachEvent(CUSTOM_CSS_CHANGE, listener); | ||
}; | ||
|
||
const detachCustomCSSChange = listener => { | ||
eventProvider.detachEvent(CUSTOM_CSS_CHANGE, listener); | ||
}; | ||
|
||
const fireCustomCSSChange = tag => { | ||
return eventProvider.fireEvent(CUSTOM_CSS_CHANGE, tag); | ||
}; | ||
|
||
const customCSSFor = {}; | ||
|
||
const addCustomCSS = (tag, css) => { | ||
if (!customCSSFor[tag]) { | ||
customCSSFor[tag] = []; | ||
} | ||
|
||
customCSSFor[tag].push(css); | ||
fireCustomCSSChange(tag); | ||
|
||
RenderScheduler.reRenderAllUI5Elements({ tag }); | ||
}; | ||
|
||
const getCustomCSS = tag => { | ||
return customCSSFor[tag] ? customCSSFor[tag].join("") : ""; | ||
}; | ||
|
||
export { addCustomCSS, getCustomCSS }; | ||
export { | ||
addCustomCSS, | ||
getCustomCSS, | ||
attachCustomCSSChange, | ||
detachCustomCSSChange, | ||
}; |
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 |
---|---|---|
@@ -1,25 +1,29 @@ | ||
import getEffectiveStyle from "./getEffectiveStyle.js"; | ||
import { attachCustomCSSChange } from "./CustomStyle.js"; | ||
|
||
const constructableStyleMap = new Map(); | ||
|
||
attachCustomCSSChange(tag => { | ||
constructableStyleMap.delete(tag); | ||
}); | ||
|
||
/** | ||
* Returns (and caches) a constructable style sheet for a web component class | ||
* Note: Chrome | ||
* @param ElementClass | ||
* @returns {*} | ||
*/ | ||
const getConstructableStyle = ElementClass => { | ||
const tagName = ElementClass.getMetadata().getTag(); | ||
const styleContent = getEffectiveStyle(ElementClass); | ||
if (constructableStyleMap.has(tagName)) { | ||
return constructableStyleMap.get(tagName); | ||
} | ||
const tag = ElementClass.getMetadata().getTag(); | ||
|
||
const style = new CSSStyleSheet(); | ||
style.replaceSync(styleContent); | ||
if (!constructableStyleMap.has(tag)) { | ||
const styleContent = getEffectiveStyle(ElementClass); | ||
const style = new CSSStyleSheet(); | ||
style.replaceSync(styleContent); | ||
constructableStyleMap.set(tag, [style]); | ||
} | ||
|
||
constructableStyleMap.set(tagName, style); | ||
return style; | ||
return constructableStyleMap.get(tag); | ||
}; | ||
|
||
export default getConstructableStyle; |
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 |
---|---|---|
@@ -1,12 +1,23 @@ | ||
import { getCustomCSS } from "./CustomStyle.js"; | ||
import { getCustomCSS, attachCustomCSSChange } from "./CustomStyle.js"; | ||
import getStylesString from "./getStylesString.js"; | ||
|
||
const effectiveStyleMap = new Map(); | ||
|
||
attachCustomCSSChange(tag => { | ||
effectiveStyleMap.delete(tag); | ||
}); | ||
|
||
const getEffectiveStyle = ElementClass => { | ||
const tag = ElementClass.getMetadata().getTag(); | ||
const customStyle = getCustomCSS(tag) || ""; | ||
|
||
const builtInStyles = getStylesString(ElementClass.styles); | ||
return `${builtInStyles} ${customStyle}`; | ||
if (!effectiveStyleMap.has(tag)) { | ||
const customStyle = getCustomCSS(tag) || ""; | ||
const builtInStyles = getStylesString(ElementClass.styles); | ||
const effectiveStyle = `${builtInStyles} ${customStyle}`; | ||
effectiveStyleMap.set(tag, effectiveStyle); | ||
} | ||
|
||
return effectiveStyleMap.get(tag); | ||
}; | ||
|
||
export default getEffectiveStyle; |
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,53 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"> | ||
<meta charset="utf-8"> | ||
|
||
<title>Button</title> | ||
|
||
<script data-ui5-config type="application/json"> | ||
{ | ||
"language": "EN", | ||
"noConflict": { | ||
"events": ["click"] | ||
}, | ||
"calendarType": "Islamic" | ||
} | ||
</script> | ||
|
||
<script src="../../webcomponentsjs/webcomponents-loader.js"></script> | ||
<script src="../../resources/bundle.esm.js" type="module"></script> | ||
<script nomodule src="../../resources/bundle.es5.js"></script> | ||
|
||
</head> | ||
|
||
<body style="background-color: var(--sapBackgroundColor);"> | ||
|
||
<ui5-select id="select"> | ||
<ui5-option>1</ui5-option> | ||
<ui5-option>2</ui5-option> | ||
</ui5-select> | ||
|
||
<br /> | ||
<br /> | ||
|
||
<ui5-button id="btn">Apply custom css and rerender</ui5-button> | ||
|
||
<br /> | ||
<br /> | ||
|
||
<script> | ||
const applyCustomCSSAndRerender = function() { | ||
window["sap-ui-webcomponents-bundle"].addCustomCSS("ui5-select", ".ui5-select-root { background-color: red; } "); | ||
}; | ||
|
||
document | ||
.getElementById("btn") | ||
.addEventListener("click", applyCustomCSSAndRerender); | ||
|
||
</script> | ||
|
||
</body> | ||
</html> |