-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update and include useImageSizes
- Loading branch information
Simon Erslev Milfred
committed
Jul 6, 2023
1 parent
0e55ca7
commit ad231e6
Showing
9 changed files
with
952 additions
and
2,043 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,3 +1,16 @@ | ||
import breakpoints from "./assets/js/breakpoints"; | ||
|
||
const mappedBreakpoints = {}; | ||
for (const [key, value] of Object.entries(breakpoints)) { | ||
mappedBreakpoints[`<${key}`] = value.px - 1; | ||
} | ||
|
||
export default defineNuxtConfig({ | ||
extends: "..", | ||
|
||
image: { | ||
screens: { | ||
...mappedBreakpoints, | ||
}, | ||
}, | ||
}); |
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,64 @@ | ||
/** | ||
* Helper to smartly fill in responsive sizes for images. | ||
* @param {string} sizes The sizes to fill in between. | ||
*/ | ||
export function useImageSizes(sizes) { | ||
const screens = { ...useImage().options.screens }; | ||
let breakpointList = [...new Set(Object.values(screens))] | ||
.map((value) => +value) | ||
.filter((value) => !isNaN(value)); | ||
breakpointList.sort((a, b) => a - b); | ||
|
||
let _return = sizes; | ||
if (sizes) { | ||
const splitSizes = sizes | ||
.split(" ") | ||
.map((val) => { | ||
if (!String(val).includes(":")) { | ||
return `${getScreenKey(Math.max(...breakpointList))}:${val.trim()}`; | ||
} | ||
return String(val).trim(); | ||
}) | ||
.filter((val) => { | ||
return Boolean(val) && val.split(":").filter(Boolean).length === 2; | ||
}); | ||
splitSizes.sort((a, b) => { | ||
a = parseInt(a.split(":")[0].replace(/\D/g, ""), 10); | ||
b = parseInt(b.split(":")[0].replace(/\D/g, ""), 10); | ||
|
||
return a - b; | ||
}); | ||
|
||
if (sizes.includes("vw") && breakpointList.length) { | ||
const output = []; | ||
|
||
for (let i = 0; i < splitSizes.length; i++) { | ||
const size = splitSizes[i].split(":")[1]; | ||
const breakpoint = parseInt( | ||
splitSizes[i].split(":")[0].replace(/\D/g, ""), | ||
10 | ||
); | ||
|
||
if (splitSizes[i].includes("vw")) { | ||
const midList = breakpointList | ||
.filter((val) => val < breakpoint) | ||
.map((val) => { | ||
return `${getScreenKey(val)}:${size}`; | ||
}); | ||
midList.length && output.push(...midList); | ||
} | ||
|
||
breakpointList = breakpointList.filter((val) => val > breakpoint); | ||
output.push(splitSizes[i]); | ||
} | ||
_return = output.join(" "); | ||
} else { | ||
_return = splitSizes.join(" "); | ||
} | ||
} | ||
return _return; | ||
|
||
function getScreenKey(size) { | ||
return Object.keys(screens).find((key) => screens[key] === size); | ||
} | ||
} |
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
Oops, something went wrong.