Skip to content

Instantly share code, notes, and snippets.

View chadmuro's full-sized avatar

Chad Murobayashi chadmuro

View GitHub Profile
@chadmuro
chadmuro / hookstate.js
Created August 27, 2023 04:57
hookstate
import React from 'react';
import { hookstate, useHookstate } from '@hookstate/core';
const globalState = hookstate(0);
setInterval(() => globalState.set(p => p + 1), 3000)
export const ExampleComponent = () => {
const state = useHookstate(globalState);
return <>
@chadmuro
chadmuro / ModalDialog.js
Created November 1, 2021 14:34
Headless UI Dialog
import { Dialog } from '@headlessui/react';
const ModalDialog = ({ isOpen, setIsOpen }) => {
return (
<Dialog open={isOpen} onClose={() => setIsOpen(false)}>
<div className="flex items-center justify-center min-h-screen">
<Dialog.Overlay className="fixed inset-0 bg-black opacity-30" />
<div className="relative bg-white rounded max-w-sm mx-auto p-8">
<Dialog.Title className="text-xl">Title of dialog</Dialog.Title>
<Dialog.Description>
@chadmuro
chadmuro / index.d.ts
Last active October 24, 2021 12:16
useState Type
/**
* Returns a stateful value, and a function to update it.
*
* @version 16.8.0
* @see https://reactjs.org/docs/hooks-reference.html#usestate
*/
function useState<S>(initialState: S | (() => S)): [S, Dispatch<SetStateAction<S>>];
// convenience overload when first argument is omitted
/**
* Returns a stateful value, and a function to update it.
@chadmuro
chadmuro / Controlled.js
Created October 18, 2021 13:05
Controlled Component
import React, { useState } from "react";
const Controlled = () => {
const [inputText, setInputText] = useState("");
const handleSubmit = (e) => {
e.preventDefault();
console.log(inputText);
};
@chadmuro
chadmuro / Uncontrolled.js
Last active October 18, 2021 13:04
Uncontrolled Component
import React, { useRef } from "react";
const Uncontrolled = () => {
const inputRef = useRef(null);
const handleSubmit = (e) => {
e.preventDefault();
console.log(inputRef.current.value);
};
@chadmuro
chadmuro / Theme.js
Created September 16, 2021 23:25
MUI extend theme
import { createTheme } from '@mui/material/styles';
const theme = createTheme({
status: {
danger: '#e53e3e',
},
palette: {
primary: {
main: '#0971f1',
darker: '#053e85',
@chadmuro
chadmuro / Theme.js
Created September 16, 2021 23:24
MUI variant theme
const theme = createTheme({
components: {
MuiButton: {
variants: [
{
props: { variant: 'dashed' },
style: {
textTransform: 'none',
border: `2px dashed grey${blue[500]}`,
},
@chadmuro
chadmuro / Example.js
Created September 16, 2021 22:51
MUI sx prop
import * as React from 'react';
import TrendingUpIcon from '@mui/icons-material/TrendingUp';
import { Box } from '@mui/system';
export default function Example() {
return (
<Box
sx={{
bgcolor: 'background.paper',
boxShadow: 1,
@chadmuro
chadmuro / FileInput.js
Created August 5, 2021 10:52
File Input
import { useState, useEffect } from 'react';
import Button from '@material-ui/core/Button';
import Box from '@material-ui/core/Box';
const FileInput = () => {
const [selectedImage, setSelectedImage] = useState(null);
const [imageUrl, setImageUrl] = useState(null);
useEffect(() => {
if (selectedImage) {
@chadmuro
chadmuro / Form.js
Last active August 1, 2021 04:04
Form with validation
import React from 'react';
import { makeStyles } from '@material-ui/core';
import TextField from '@material-ui/core/TextField';
import Button from '@material-ui/core/Button';
import { useForm, Controller } from 'react-hook-form';
const useStyles = makeStyles(theme => ({
root: {
display: 'flex',
flexDirection: 'column',