Skip to content

Instantly share code, notes, and snippets.

View rimzzlabs's full-sized avatar
🛵
Riding ...

Rizki Citra (rimzzlabs) rimzzlabs

🛵
Riding ...
View GitHub Profile
const navbar = document.querySelector(".navbar"),
sectionKelas = document.querySelectorAll(".section-kelas");
// initialize IntersectionObserver
const io = new IntersectionObserver((entries) => {
// entries are array of element e.g: [<div></div>, <p></p>]
// loop through the element
entries.forEach((entry) => {
// assign the data set attribute
const kelas = entry.target.dataset.kelas;
@rimzzlabs
rimzzlabs / capsLetter.js
Last active June 10, 2022 02:41
A JavaScript Function that will capitalize the first letter of each words
export const capsLetter = (str) => str.split(' ').map(l => l.slice(0, 1).toUpperCase() + l.slice(1)).join(' ')
@rimzzlabs
rimzzlabs / useTheme.ts
Last active September 26, 2022 07:33
custom hook to manage theme
import { useCallback, useEffect, useState, } from 'react'
type Theme = 'dark' | 'light'
export default function useTheme(){
const [theme, setTheme] = useState<Theme>('light')
const changeTheme = useCallback((payload: Theme) => {
return () => {
setTheme(payload)
@rimzzlabs
rimzzlabs / tailwind.config.js
Created January 28, 2022 16:26
tailwind config for create react app
module.exports = {
content: ['./src/**/*.{js,jsx,ts,tsx}', './public/index.html'],
darkMode: 'class',
theme: {
extend: {},
}
plugins: []
}
@rimzzlabs
rimzzlabs / Image.tsx
Last active September 26, 2022 07:29
next/image without explicit height and width
import Image from "next/image"
export function ImageComponent(){
return (
<figure className="relative w-40 h-40 md:w-80 md:h-80">
<Image lazy layout="fill" />
</figure>
)
}
@rimzzlabs
rimzzlabs / httpreq.ts
Last active October 7, 2022 16:03
Avoid async / await try - catch hell
export async function httpreq(fn: () => Promise<any>): Promise<[any, any]> {
try {
const res = await fn()
return [res, null]
} catch(err) {
return [null, err]
}
}
@rimzzlabs
rimzzlabs / toElipsis.ts
Last active September 26, 2022 07:45
given number length, make the string ellipsis
export const toElipsis = (str: string, range?: = 10): string => {
return str.slice(0, range) + '...'
}
// usage
const elipsis = toElipsis("Please mister, call me Robin, I will be your company while at your trip to Mongo Island", 44)
console.info(elipsis) // -> 'Please mister, call me robin, I will be your...'
@rimzzlabs
rimzzlabs / extensions.json
Last active October 2, 2022 19:04
My Personal VS Code Extension
{
"recommendations": [
"bradlc.vscode-tailwindcss",
"eamodio.gitlens",
"dbaeumer.vscode-eslint",
"usernamehw.errorlens",
"vscode-icons-team.vscode-icons",
"mintlify.document",
"esbenp.prettier-vscode",
"mikestead.dotenv",
@rimzzlabs
rimzzlabs / .prettierrc.js
Created October 2, 2022 19:05
Prettier Config
module.exports = {
semi: false,
tabWidth: 2,
printWidth: 120,
singleQuote: true,
jsxSingleQuote: true,
trailingComma: 'none',
arrowParens: 'always',
endOfLine: 'auto',
}
type Value = Record<string, unknown>
export const arrayObjSearch = <T extends Value>(arr: Array<T>, key: keyof Array<T>[0], target: string): T | null => {
if (!key) throw new TypeError('You should provide what key to find')
if (arr.length === 0) return null
let i = 0
do {
const source = arr[i][key]