From a0470519d74cebb70b5699e9f5c9dd4982fcabe3 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Fri, 11 Sep 2020 19:07:54 +0300 Subject: [PATCH] Add support for dark theme. Fixes #17 --- README.md | 5 ++++- web/index.html | 2 +- web/src/index.js | 53 ++++++++++++++++++++++++++++++++++++-------- web/style/index.css | 2 +- web/style/index.sass | 49 ++++++++++++++++++++++++++++++++-------- 5 files changed, 90 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 58b5774..c46f787 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ This requires logging in with your account instead of a bot token. "stickerpicker": { "content": { "type": "m.stickerpicker", - "url": "https://your.sticker.picker.url/index.html", + "url": "https://your.sticker.picker.url/?theme=$theme", "name": "Stickerpicker", "data": {} }, @@ -44,6 +44,9 @@ This requires logging in with your account instead of a bot token. If you do not yet have a `m.widgets` event, simply create it with that content. You can also [use the client-server API directly][1] instead of using Element Web. + + The `theme=$theme` query parameter will make the widget conform to Element's theme automatically. + You can also use `light`, `dark` or `black` instead of `$theme` to always use a specific theme. 3. Open the sticker picker and enjoy the fast sticker picking experience. [1]: https://matrix.org/docs/spec/client_server/latest#put-matrix-client-r0-user-userid-account-data-type diff --git a/web/index.html b/web/index.html index 352f0b8..50bd7c1 100644 --- a/web/index.html +++ b/web/index.html @@ -2,7 +2,7 @@ - + Maunium sticker picker diff --git a/web/src/index.js b/web/src/index.js index cd165dd..625ed00 100644 --- a/web/src/index.js +++ b/web/src/index.js @@ -20,14 +20,23 @@ const makeThumbnailURL = mxc => `${HOMESERVER_URL}/_matrix/media/r0/thumbnail/${ // This is also used to fix scrolling to sections on Element iOS const isMobileSafari = navigator.userAgent.match(/(iPod|iPhone|iPad)/) && navigator.userAgent.match(/AppleWebKit/) +export const parseQuery = str => Object.fromEntries( + str.split("&") + .map(part => part.split("=")) + .map(([key, value = ""]) => [key, value])) + +const supportedThemes = ["light", "dark", "black"] + class App extends Component { constructor(props) { super(props) + this.defaultTheme = parseQuery(location.search.substr(1)).theme this.state = { packs: [], loading: true, error: null, stickersPerRow: parseInt(localStorage.mauStickersPerRow || "4"), + theme: localStorage.mauStickerThemeOverride || this.defaultTheme, frequentlyUsed: { id: "frequently-used", title: "Frequently used", @@ -35,6 +44,12 @@ class App extends Component { stickers: [], }, } + if (!supportedThemes.includes(this.state.theme)) { + this.state.theme = "light" + } + if (!supportedThemes.includes(this.defaultTheme)) { + this.defaultTheme = "light" + } this.stickersByID = new Map(JSON.parse(localStorage.mauFrequentlyUsedStickerCache || "[]")) this.state.frequentlyUsed.stickers = this._getStickersByID(this.state.frequentlyUsed.stickerIDs) this.imageObserver = null @@ -73,6 +88,16 @@ class App extends Component { this.packListRef.scrollTop = this.packListRef.scrollHeight } + setTheme(theme) { + if (theme === "default") { + delete localStorage.mauStickerThemeOverride + this.setState({ theme: this.defaultTheme }) + } else { + localStorage.mauStickerThemeOverride = theme + this.setState({ theme: theme }) + } + } + reloadPacks() { this.imageObserver.disconnect() this.sectionObserver.disconnect() @@ -189,21 +214,22 @@ class App extends Component { } render() { + const theme = `theme-${this.state.theme}` if (this.state.loading) { - return html`
<${Spinner} size=${80} green />
` + return html`
<${Spinner} size=${80} green />
` } else if (this.state.error) { - return html`
+ return html`

Failed to load packs

${this.state.error}

` } else if (this.state.packs.length === 0) { - return html`

No packs found 😿

` + return html`

No packs found 😿

` } - return html`
+ return html`
this.packListRef = elem}> <${Pack} pack=${this.state.frequentlyUsed} send=${this.sendSticker} /> @@ -225,6 +251,15 @@ const Settings = ({ app }) => html` value=${app.state.stickersPerRow} onInput=${evt => app.setStickersPerRow(evt.target.value)} />
+
+ + +
` @@ -237,12 +272,12 @@ const scrollToSection = (evt, id) => { evt.preventDefault() } -const NavBarItem = ({ pack, iconOverride = null, altOverride = null }) => html` +const NavBarItem = ({ pack, iconOverride = null }) => html` scrollToSection(evt, pack.id)) : undefined}> -
+
${iconOverride ? html` - ${altOverride} + ` : html` ${pack.stickers[0].body} diff --git a/web/style/index.css b/web/style/index.css index 68cec7b..b7b2aec 100644 --- a/web/style/index.css +++ b/web/style/index.css @@ -1 +1 @@ -*{font-family:sans-serif}body{margin:0}h1{font-size:1rem}:root{--stickers-per-row: 4;--sticker-size: calc(100vw / var(--stickers-per-row))}main.spinner{margin-top:5rem}main.error,main.empty{margin:2rem}main.empty{text-align:center}main.has-content{position:fixed;top:0;left:0;right:0;bottom:0;display:grid;grid-template-rows:calc(12vw + 2px) auto}nav{display:flex;overflow-x:auto;height:calc(12vw + 2px);background-color:white}nav>a{border-bottom:2px solid transparent}nav>a.visible{border-bottom-color:green}nav>a>div.sticker{width:12vw;height:12vw}nav>a>div.sticker.icon>img{width:70%;height:70%;padding:15%}div.pack-list,nav{scrollbar-width:none}div.pack-list::-webkit-scrollbar,nav::-webkit-scrollbar{display:none}div.pack-list{overflow-y:auto}div.pack-list.ios-safari-hack{position:fixed;top:calc(12vw + 2px);bottom:0;left:0;right:0;-webkit-overflow-scrolling:touch}section.stickerpack{margin-top:.75rem}section.stickerpack>div.sticker-list{display:flex;flex-wrap:wrap}section.stickerpack>h1{margin:0 0 0 .75rem}div.sticker{display:flex;padding:4px;cursor:pointer;position:relative;width:var(--sticker-size);height:var(--sticker-size);box-sizing:border-box}div.sticker:hover{background-color:#eee}div.sticker>img{display:none;width:100%;object-fit:contain}div.sticker>img.visible{display:initial}div.settings-list{display:flex;flex-direction:column}div.settings-list>*{margin:.5rem}div.settings-list button{padding:.5rem;border-radius:.25rem}div.settings-list input{width:100%} +*{font-family:sans-serif}body{margin:0}h1{font-size:1rem}:root{--stickers-per-row: 4;--sticker-size: calc(100vw / var(--stickers-per-row))}main{color:var(--text-color)}main.spinner{margin-top:5rem}main.error,main.empty{margin:2rem}main.empty{text-align:center}main.has-content{position:fixed;top:0;left:0;right:0;bottom:0;display:grid;grid-template-rows:calc(12vw + 2px) auto}main.theme-light{--highlight-color: #eee;--text-color: black;background-color:white}main.theme-dark{--highlight-color: #444;--text-color: white;background-color:#22262e}main.theme-black{--highlight-color: #222;--text-color: white;background-color:black}.icon{width:100%;height:100%;background-color:var(--text-color);mask-size:contain;-webkit-mask-size:contain;mask-image:var(--icon-image);-webkit-mask-image:var(--icon-image)}.icon.icon-settings{--icon-image: url(../res/settings.svg)}.icon.icon-recent{--icon-image: url(../res/recent.svg)}nav{display:flex;overflow-x:auto}nav>a{border-bottom:2px solid transparent}nav>a.visible{border-bottom-color:green}nav>a>div.sticker{width:12vw;height:12vw}div.pack-list,nav{scrollbar-width:none}div.pack-list::-webkit-scrollbar,nav::-webkit-scrollbar{display:none}div.pack-list{overflow-y:auto}div.pack-list.ios-safari-hack{position:fixed;top:calc(12vw + 2px);bottom:0;left:0;right:0;-webkit-overflow-scrolling:touch}section.stickerpack{margin-top:.75rem}section.stickerpack>div.sticker-list{display:flex;flex-wrap:wrap}section.stickerpack>h1{margin:0 0 0 .75rem}div.sticker{display:flex;padding:4px;cursor:pointer;position:relative;width:var(--sticker-size);height:var(--sticker-size);box-sizing:border-box}div.sticker:hover{background-color:var(--highlight-color)}div.sticker>img{display:none;width:100%;object-fit:contain}div.sticker>img.visible{display:initial}div.sticker>.icon{width:70%;height:70%;margin:15%}div.settings-list{display:flex;flex-direction:column}div.settings-list>*{margin:.5rem}div.settings-list button{padding:.5rem;border-radius:.25rem}div.settings-list input{width:100%} diff --git a/web/style/index.sass b/web/style/index.sass index d7884d6..4936836 100644 --- a/web/style/index.sass +++ b/web/style/index.sass @@ -23,6 +23,8 @@ $nav-height: calc(#{$nav-sticker-size} + #{$nav-bottom-highlight}) $nav-height-inverse: calc(-#{$nav-sticker-size} - #{$nav-bottom-highlight}) main + color: var(--text-color) + &.spinner margin-top: 5rem @@ -42,25 +44,49 @@ main display: grid grid-template-rows: $nav-height auto +main.theme-light + --highlight-color: #eee + --text-color: black + background-color: white + +main.theme-dark + --highlight-color: #444 + --text-color: white + background-color: #22262e + +main.theme-black + --highlight-color: #222 + --text-color: white + background-color: black + +.icon + width: 100% + height: 100% + background-color: var(--text-color) + mask-size: contain + -webkit-mask-size: contain + mask-image: var(--icon-image) + -webkit-mask-image: var(--icon-image) + + &.icon-settings + --icon-image: url(../res/settings.svg) + + &.icon-recent + --icon-image: url(../res/recent.svg) + nav display: flex overflow-x: auto - height: $nav-height - - background-color: white - > a border-bottom: $nav-bottom-highlight solid transparent + &.visible border-bottom-color: green + > div.sticker width: $nav-sticker-size height: $nav-sticker-size - > div.sticker.icon > img - width: 70% - height: 70% - padding: 15% div.pack-list, nav scrollbar-width: none @@ -99,7 +125,7 @@ div.sticker box-sizing: border-box &:hover - background-color: #eee + background-color: var(--highlight-color) > img display: none @@ -109,6 +135,11 @@ div.sticker &.visible display: initial + > .icon + width: 70% + height: 70% + margin: 15% + div.settings-list display: flex flex-direction: column