-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(UserAvatar): add
srcset
support (#641)
- Loading branch information
Showing
11 changed files
with
157 additions
and
15 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
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,24 @@ | ||
import {getClosestNumber} from './getClosestNumber'; | ||
import {getSrcSet} from './getSrcSet'; | ||
import {SrcSetType} from './types'; | ||
import {SIZES} from '../constants'; | ||
import type {UserAvatarSize} from '../types'; | ||
|
||
export function getAvatarSrcSet( | ||
size: UserAvatarSize, | ||
sizes: Record<number, string>, | ||
{multipliers = [1, 2, 3, 4]} = {}, | ||
) { | ||
const availableSizes = Object.keys(sizes) | ||
.map((size) => Number(size)) | ||
.sort((a, b) => a - b); | ||
const baseSize = SIZES[size]; | ||
const srcSet = multipliers.map((multiplier) => { | ||
const targetSize = multiplier * baseSize; | ||
const appropriateSize = getClosestNumber(targetSize, availableSizes); | ||
|
||
return [sizes[appropriateSize], `${multiplier}x`] as const; | ||
}); | ||
|
||
return getSrcSet(srcSet as SrcSetType<typeof multipliers>); | ||
} |
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,26 @@ | ||
export function getClosestNumber(targetNumber: number, availableNumbers: number[]) { | ||
const stack = [...availableNumbers]; | ||
let previousNumber: number | undefined; | ||
|
||
while (stack.length) { | ||
const currentNumber = stack.pop() as number; | ||
const isBigger = targetNumber > currentNumber; | ||
const isSame = targetNumber === currentNumber; | ||
|
||
if (isSame) { | ||
return currentNumber; | ||
} | ||
|
||
if (isBigger) { | ||
if (previousNumber) { | ||
return previousNumber; | ||
} | ||
|
||
return currentNumber; | ||
} | ||
|
||
previousNumber = currentNumber; | ||
} | ||
|
||
return previousNumber as number; | ||
} |
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,19 @@ | ||
import type {SrcSetType} from './types'; | ||
|
||
export function getSrcSet<T>(srcSet: SrcSetType<T>) { | ||
let srcSetString = ''; | ||
|
||
for (const item of srcSet) { | ||
let part = ''; | ||
|
||
if (typeof item === 'string') { | ||
part = item; | ||
} else { | ||
part = item.join(' '); | ||
} | ||
|
||
srcSetString = `${srcSetString}${srcSetString === '' ? '' : ', '}${part}`; | ||
} | ||
|
||
return srcSetString; | ||
} |
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,11 @@ | ||
type SrcSetUrlItem = string; | ||
|
||
type SrcSetWidthDescriptor<T> = T extends `${infer _A}w` ? T : never; | ||
type SrcSetWidthItem<T> = [string, SrcSetWidthDescriptor<T>]; | ||
type SrcSetWidthType<T> = SrcSetWidthItem<T> | SrcSetUrlItem; | ||
|
||
type SrcSetDensityDescriptor<T> = T extends `${infer _A}x` ? T : never; | ||
type SrcSetDensityItem<T> = [string, SrcSetDensityDescriptor<T>]; | ||
type SrcSetDensityType<T> = SrcSetDensityItem<T> | SrcSetUrlItem; | ||
|
||
export type SrcSetType<T> = SrcSetWidthType<T>[] | SrcSetDensityType<T>[]; |
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,9 @@ | ||
import type {UserAvatarSize} from './types'; | ||
|
||
export const SIZES: Record<UserAvatarSize, number> = { | ||
xs: 24, | ||
s: 28, | ||
m: 32, | ||
l: 42, | ||
xl: 50, | ||
}; |
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 +1,3 @@ | ||
export * from './UserAvatar'; | ||
export type {UserAvatarSize} from './types'; | ||
export {SIZES as AVATAR_SIZES} from './constants'; |
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 @@ | ||
export type UserAvatarSize = 'xs' | 's' | 'm' | 'l' | 'xl'; |