Skip to content

Instantly share code, notes, and snippets.

View KacperKozak's full-sized avatar

Kacper Kozak KacperKozak

View GitHub Profile
// ludzie-iza.json
{
"name": "Izabela",
"age": 25,
"city": "Warsaw",
"description": {
"pl": "Jestem programistką",
"en": "I'm a programmer",
"de": "Ich bin Programmiererin"
}
@KacperKozak
KacperKozak / useObject.ts
Created September 5, 2023 13:15
`useObject` hook
import { useReducer } from 'react';
type Reducer<T> = (state: T, update: Partial<T>) => T;
export const useObject = <T>(initial: T) => {
return useReducer<Reducer<T>>((state, update) => ({ ...state, ...update }), initial);
};
@KacperKozak
KacperKozak / Tab.tsx
Created April 3, 2023 11:22
React tabs
import React, { ReactNode } from 'react'
export interface TabProps {
name: string
isActive?: boolean
children: ReactNode
}
export const Tab = ({ children }: TabProps) => <>{children}</>
@KacperKozak
KacperKozak / Portal.tsx
Last active June 13, 2023 09:07
Very simple React portal implementation with context
import { createContext, PropsWithChildren, useContext, useState } from 'react'
import { createPortal } from 'react-dom'
const PortalContext = createContext<HTMLDivElement | null>(null)
export const PortalContextProvider = ({ children }: PropsWithChildren) => {
const [element, setElement] = useState<HTMLDivElement | null>(null)
return (
<>
@KacperKozak
KacperKozak / useSet.ts
Last active February 16, 2022 21:14
React useSet hook
import { uniq, xor } from 'lodash'
import { useMemo, useState } from 'react'
export const useSet = <T>(initial: T[] | (() => T[]) = []) => {
const [state, setState] = useState(initial)
return useMemo(
() => ({
values: state,
size: state.length,
@KacperKozak
KacperKozak / onClickOutside.ts
Created February 15, 2022 16:54
React useOnClickOutside hook
import { MutableRefObject, useEffect } from 'react'
export const useOnClickOutside = (refs: MutableRefObject<any>[], handler: () => void) => {
useEffect(() => {
const listener = (event: any) => {
const someElContainsTarget = refs.some((ref) =>
ref.current?.contains(event.target),
)
if (!someElContainsTarget) handler()
}
@KacperKozak
KacperKozak / useStateEffect.ts
Last active March 18, 2022 08:30
useStateEffect - `useState` with dependency list, similar to `useMemo` but you can change state
import { DependencyList, Dispatch, SetStateAction, useEffect, useRef, useState } from 'react'
/**
* `useState` with dependency list, similar to `useMemo` but you can change state
*
* @example const [error, setError] = useStateEffect(!url, [url]);
* @example const [table, setTable] = useStateEffect(() => makeTable(data), [data]);
*/
export const useStateEffect = <T>(
value: T | (() => T),
@KacperKozak
KacperKozak / plink-plonk.js
Created February 15, 2020 10:37 — forked from tomhicks/plink-plonk.js
Listen to your web pages
@KacperKozak
KacperKozak / App.tsx
Last active November 12, 2019 18:49
Simple form hook validation for React
interface FormData {
message: string
email: string
}
const App = () => {
const { submitHandler, values, updateFieldValue, touchField, getFieldError } = useForm<FormData>(
{
message: required('Message is required'),
email: pipe(
@KacperKozak
KacperKozak / translate.sh
Created April 15, 2016 10:12
Translate selected text
#!/usr/bin/env bash
notify-send --icon=info "$(xsel -o)" "$(wget -U "Mozilla/5.0" -qO - "http://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl=pl&dt=t&q=$(xsel -o | sed "s/[\"'<>]//g")" | sed "s/,,,0]],,.*//g" | awk -F'"' '{print $2, $6}')"