Skip to content

Commit

Permalink
fix: corrected URL redirect for backwards compatability (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
NateBaldwinDesign authored May 9, 2022
1 parent 4d6e791 commit 1e5bb58
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/ui/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ posthtml()
.then((result) => result);


// Redirect for URL parameters
let url = new URL(window.location);
let params = new URLSearchParams(url.search.slice(1));
console.log(url.href)

if(params.has('colorKeys') || params.has('name')) {
let newURL = url.href.replace('index', 'theme')
window.location.replace(newURL);
}


window.matchMedia('(prefers-color-scheme: dark)')
.addEventListener('change', event => {
if (event.matches) {
Expand Down
22 changes: 22 additions & 0 deletions packages/ui/src/js/params.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,28 @@ function paramSetup() {
});
}
else if(params.has('colorKeys')) {
// old way used #, but now it's seen as a hash.
// Have to replace # with character code and reset URL
if(window.location.hash) {
let hash = window.location.hash.toString();
// let newParam = hash.replaceAll(`#`, `%23`).replaceAll(`,`, `%54`);
let paramArray = hash.split('&');
console.log(paramArray)
let paramOptions = ['base', 'mode', 'ratios'];
paramArray.map((p) => {
for(let i = 0; i < paramOptions.length; i++) {
if(p.includes(paramOptions[i])) {
// strip string to reveal parameters
let value = p.replace(`${paramOptions[i]}=`, '');
params.set(`${paramOptions[i]}`, value)
}
}
})

params.set('colorKeys', paramArray[0])
window.history.replaceState({}, '', '?' + params); // update the page's URL.
}

let colorKeys = Promise.resolve(params.get('colorKeys').split(','));
let colorspace = Promise.resolve(params.get('mode'));
let ratios = Promise.resolve(params.get('ratios').split(',').map((r) => {return Number(r)}));
Expand Down

0 comments on commit 1e5bb58

Please sign in to comment.