Skip to content

Instantly share code, notes, and snippets.

@mayognaise
mayognaise / set-aria-live.ts
Last active September 28, 2023 21:56
Set `aria-live` manually here as we cannot use Swiper's navigation since we use custom arrows.
/**
* Set `aria-live` manually here as we cannot use Swiper's navigation since we use custom arrows.
*/
function setAriaLive(el: HTMLDivElement | null, value: string) {
if (!el) return;
el.innerHTML = '';
setTimeout(() => {
el.innerHTML = `<div aria-live="polite" aria-atomic="true" className="visually-hidden">${value}</div>`;
}, 100);
}
@mayognaise
mayognaise / null-undefined.ts
Last active July 27, 2023 06:21
Define keys as null or optional
// Define either undefined or null
type Maybe<T> = T | undefined | null;
type User = {
name: string
email: string
phone: Maybe<string>
}
/**
@mayognaise
mayognaise / all-optional.ts
Created July 27, 2023 06:11
Define all keys as optional
// When all keys are optional
type User = Partial<{
name: string
email: string
phone: string
}>
/**
type User = {
name?: string | undefined;
@mayognaise
mayognaise / optional-key.ts
Created July 27, 2023 06:10
Define a key as optional
// When one key is optional
type User = {
name: string
email: string
phone?: string
}
/**
type User = {
name: string;
@mayognaise
mayognaise / create-union-from-object-array.ts
Last active July 26, 2023 08:37
Create union type from object array
const sizes = [
{ variant: 'sm', value: 8 },
{ variant: 'md', value: 16 },
{ variant: 'lg', value: 24 },
] as const
type SizeKey = keyof typeof sizes[number] // "variant" | "value"
type SizeVariant = typeof sizes[number]['variant'] // "sm" | "md" | "lg"
type SizeValue = typeof sizes[number]['value'] // 8 | 16 | 24
@mayognaise
mayognaise / create-union-from-object-value.ts
Created July 26, 2023 08:10
Create a union type from object value
const colors = {
bg: '#e5e5e5',
navy: '#002b45',
orange: '#fb5a3b',
darkOrange: '#cf4d34',
} as const
type ColorValue = typeof colors[keyof typeof colors] // "#e5e5e5" | "#002b45" | "#fb5a3b" | "#cf4d34"
@mayognaise
mayognaise / create-union-from-object-key.ts
Last active July 26, 2023 08:06
Create a union type from object key
const colors = {
bg: '#e5e5e5',
navy: '#002b45',
orange: '#fb5a3b',
darkOrange: '#cf4d34',
} as const
type ColorKey = keyof typeof colors // "navy" | "orange" | "bg" | "darkOrange"
@mayognaise
mayognaise / use-boolean.ts
Last active July 18, 2023 17:09
A hook to manage boolean
/**
A hook to manage boolean
@example
function Example() {
const [flag, setFlag] = useBoolean()
return (
<>
<p>Boolean state: {flag}</p>
<button onClick={setFlag.on}>On</button>
@mayognaise
mayognaise / mask-gradient.css
Created July 13, 2023 23:56
Mask gradient css
.mask-gradient-bottom {
mask-image: linear-gradient(to bottom, black 2%, transparent 98%);
}
.mask-gradient-top {
mask-image: linear-gradient(to top, black 2%, transparent 98%);
}
@mayognaise
mayognaise / line-clamp.css
Last active July 13, 2023 23:56
Clamp for multiple lines (three dots when text is too long)
.line-clamp {
-webkit-box-orient: vertical;
-webkit-line-clamp: 2; /* max line length */
display: -webkit-box;
overflow: hidden;
text-overflow: ellipsis;
}