Skip to content

Instantly share code, notes, and snippets.

View serifcolakel's full-sized avatar

Serif COLAKEL serifcolakel

View GitHub Profile
/**
* @description This is a test for the `InferValueFromColor` type.
* @example
* type Color = '#fff';
* type Value = InferValueFromColor<Color>; // Value is 'fff'
* @example
* type Color = '#fff' | '#000';
* type Value = InferValueFromColor<Color>; // Value is 'fff' | '000'
* @example
* type Color = '#fff' | '#000' | '#aaa';
This file has been truncated, but you can view the full file.
[
{
"id": "1f600",
"name": "grinning face",
"shortName": ":grinning:",
"fallback": "😀",
"type": "STANDARD",
"category": "PEOPLE",
"order": 10001,
"representation": {
@serifcolakel
serifcolakel / getColor.ts
Created December 14, 2023 21:47
Extracts the color value from a string that follows the format `text-{color}-500`.
const colors = ["text-yellow-500", "text-violet-500", "text-red-500"] as const;
/**
* Extracts the color value from a string that follows the format `text-{color}-500`.
* @template S The input string.
* @param {S} input The input string to extract the color from.
* @returns {ExtractColor<S>} The extracted color value.
*/
type ExtractColor<S extends string> = S extends `text-${infer Color}-500`
? Color
@serifcolakel
serifcolakel / store.ts
Last active May 20, 2024 07:25
Make global state management with useSyncExternalStore hook. Idea from Jotai. i'm calling jotai-clone for this approach. Finally, i'm try on my side project it works very well :)
/**
* full article about this gist
* @link https://blog.stackademic.com/building-a-state-management-system-with-react-86d382fa33cd
*/
import { useSyncExternalStore } from 'react';
/**
* @description An atom is a unit of state in Jotai. It is a container of a value that can be read synchronously and updated asynchronously.
*/
@serifcolakel
serifcolakel / FormElement.tsx
Created December 6, 2023 20:05
These form elements can include buttons, links, inputs, selects, and text areas, each with various styling options. Instead of creating separate components for each form element, we can build a polymorphic React FormElement component.
import React from 'react';
type FormElementBaseProps = {
children?: React.ReactNode;
variant?: 'primary' | 'secondary' | 'outline' | 'link';
};
type FormElementProps = FormElementBaseProps &
(
| (React.ButtonHTMLAttributes<HTMLButtonElement> & {
const data = {
NaMe: "name",
aGe: 12,
a: "mask",
};
export type TLiteral = "UPPER" | "LOWER" | "CAPITALIZE" | "UNCAPITALIZE";
export type TransformKeys<T, L extends TLiteral> = {
[K in keyof T as L extends "UPPER"
import { test } from 'vitest';
import { useLocalStorage } from './useLocalStorage';
const EMPTY_VALUE = undefined;
test('useLocalStorage hook with string', async ({ expect }) => {
const TEST_OPTION = {
KEY: 'test-key-string',
VALUE: 'test-value',
};
export type TUseLocalStorage<T> = [
() => T | undefined,
(value: T) => void,
() => void
];
/**
* @author Serif Colakel
* @name useLocalStorage
* @description See the test for this hook {@link https://gist.github.com/serifcolakel/44cb296dd5cb8983a5e2deb14d962878 Gist Link}
/* eslint-disable react/jsx-no-useless-fragment */
import React, { type ReactNode } from 'react';
/**
* @description A component that conditionally renders its children based on a given condition and optional limit.
*/
interface ConditionalRenderProps {
/**
* @description The condition to render the children.
* @default false
import { RefObject, useEffect, useRef } from 'react';
function useEventListener<K extends keyof MediaQueryListEventMap>(
eventName: K,
handler: (event: MediaQueryListEventMap[K]) => void,
element: RefObject<MediaQueryList>,
options?: boolean | AddEventListenerOptions,
): void;
function useEventListener<K extends keyof WindowEventMap>(