Skip to content

Instantly share code, notes, and snippets.

View ruanvalente's full-sized avatar
🎯
Focusing

Ruan Valente ruanvalente

🎯
Focusing
View GitHub Profile
@ruanvalente
ruanvalente / useListener.tsx
Created November 26, 2024 17:53
useListener.tsx
import React from 'react';
export default function useListener<T>(eventType: string): T | null {
const [eventData, setEventData] = React.useState<T | null>(null);
React.useEffect(() => {
const handleEvent = (event: Event) => {
if (event.type === eventType) {
setEventData((event as CustomEvent<T>).detail)
}
@ruanvalente
ruanvalente / useEmit.tsx
Created November 26, 2024 17:46
useEmit
type EmitFunction<T> = (eventType: string, detail: T) => void;
const useEmit = <T,>(): EmitFunction<T> => {
const emit: EmitFunction<T> = (eventType: string, detail: T) => {
const customEvent = new CustomEvent(eventType, {
detail
})
window.dispatchEvent(customEvent)
}
@ruanvalente
ruanvalente / Condition.tsx
Created November 26, 2024 17:34
ConditionComponent
import React, { ReactNode } from 'react';
interface ConditionProps {
when: boolean;
children: ReactNode;
else?: ReactNode;
}
export function Condition({ when, children, else: elseContent }: ConditionProps) {
if (when) {
@ruanvalente
ruanvalente / For.tsx
Last active November 26, 2024 17:33
ForComponent
import React, { ReactNode } from 'react';
interface ForProps<T> {
each: T[];
render: (item: T, index: number) => ReactNode;
}
export function For<T>({ each, render }: ForProps<T>) {
return <>{each.map((item, index) => render(item, index))}</>;
}
@ruanvalente
ruanvalente / web-servers.md
Created February 1, 2017 02:26 — forked from willurd/web-servers.md
Big list of http static server one-liners

Each of these commands will run an ad hoc http static server in your current (or specified) directory, available at http://localhost:8000. Use this power wisely.

Discussion on reddit.

Python 2.x

$ python -m SimpleHTTPServer 8000