Skip to content

Instantly share code, notes, and snippets.

View KacperKozak's full-sized avatar

Kacper Kozak KacperKozak

View GitHub Profile
#!/bin/bash
if [ -e $1 ]
then
echo "Podaj rozmiar jako pierwszy argument"
else
if [ -e $2 ]
then
@KacperKozak
KacperKozak / AllegroLinks.html
Created March 17, 2014 09:31
Linki na aukcje Allegro, wystarczy zamienić 000... na ID użytkowania.
@KacperKozak
KacperKozak / ctrl-enter.js
Last active May 4, 2024 12:24
[Ctrl] + [Enter] to submit forms
document.body.addEventListener('keydown', (event) => {
if(event.key === "Enter" && (event.metaKey || event.ctrlKey)) {
event.target.form?.submit();
}
});
@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}')"
@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 / plink-plonk.js
Created February 15, 2020 10:37 — forked from tomhicks/plink-plonk.js
Listen to your web pages
@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 / 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 / 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 / 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 (
<>