23
loading...
This website collects cookies to deliver better user experience
import React, { useRef } from 'react'
function Button ({ label, action }) {
// declare & initializing a reference to null
const buttonRef = useRef(null)
// attaching 'buttonRef' to the <button> element in JSX
return (
<button onClick={action} ref={buttonRef}>{label}</button>
)
}
}
import React, { useState } from "react";
const InputModal = ({ close }) => {
const [value, updateVal] = useState("");
const onChange = (e) => {
updateVal(e.target.value);
};
const onSubmit = (e) => {
e.preventDefault();
close();
};
return (
<div className="overlay">
<div className="modal">
<h1>Insert a new value</h1>
<form action="?" onSubmit={onSubmit}>
<input type="text" onChange={onChange} value={value} />
<button>Save new value</button>
</form>
</div>
</div>
);
};
export default InputModal;
import React, { useState, useRef } from "react";
const InputModal = ({ close }) => {
const [value, updateVal] = useState("");
const inputRef = useRef(null);
const onChange = (e) => {
updateVal(e.target.value);
};
const onSubmit = (e) => {
e.preventDefault();
close();
};
return (
<div className="overlay">
<div className="modal">
<h1>Insert a value</h1>
<form action="?" onSubmit={onSubmit}>
<input type="text" onChange={onChange} value={value} ref={inputRef} />
<button>Save</button>
</form>
</div>
</div>
);
};
export default InputModal;
import React, { useState, useRef, useEffect } from "react";
const InputModal = ({ close }) => {
const [value, updateVal] = useState("");
const inputRef = useRef(null);
useEffect(() => {
inputRef.current.focus();
}, []);
const onChange = (e) => {
updateVal(e.target.value);
};
const onSubmit = (e) => {
e.preventDefault();
close();
};
return (
<div className="overlay">
<div className="modal">
<h1>Insert a value</h1>
<form action="?" onSubmit={onSubmit}>
<input type="text" onChange={onChange} value={value} ref={inputRef} />
<button>Save</button>
</form>
</div>
</div>
);
};
export default InputModal;
import React, { useState, useRef, useEffect } from "react";
const InputModal = ({ close }) => {
const [value, updateVal] = useState("");
const inputRef = useRef(null);
const modalRef = useRef(null);
const onClickOverlay = (e) => {
const overlay = e.target;
if (modalRef.current && !modalRef.current.contains(overlay)) {
e.preventDefault();
e.stopPropagation();
close();
}
};
useEffect(() => {
inputRef.current.focus();
document.body.addEventListener("click", onClickOverlay);
}, []);
const onChange = (e) => {
updateVal(e.target.value);
};
const onSubmit = (e) => {
e.preventDefault();
close();
};
return (
<div className="overlay">
<div className="modal" ref={modalRef}>
<h1>Insert a value</h1>
<form action="?" onSubmit={onSubmit}>
<input type="text" onChange={onChange} value={value} ref={inputRef} />
<button>Save</button>
</form>
</div>
</div>
);
};
export default InputModal;
import React, { useState, useRef, useEffect } from "react";
import gsap from "gsap";
const InputModal = ({ close }) => {
const [value, updateVal] = useState("");
const inputRef = useRef(null);
const modalRef = useRef(null);
const overlayRef = useRef(null);
const onComplete = () => {
inputRef.current.focus();
};
const gaspTimeline = gsap.timeline({ paused: true, onComplete });
const onClickOverlay = (e) => {
const overlay = e.target;
if (modalRef.current && !modalRef.current.contains(overlay)) {
e.preventDefault();
e.stopPropagation();
close();
}
};
useEffect(() => {
//timeline - gasp
gaspTimeline
.from(overlayRef.current, {
duration: 0.25,
autoAlpha: 0
})
.from(modalRef.current, {
duration: 0.25,
autoAlpha: 0,
y: 25
});
gaspTimeline.play();
document.body.addEventListener("click", onClickOverlay);
}, []);
const onChange = (e) => {
updateVal(e.target.value);
};
const onSubmit = (e) => {
e.preventDefault();
close();
};
return (
<div className="overlay" ref={overlayRef}>
<div className="modal" ref={modalRef}>
<h1>Insert a value</h1>
<form action="?" onSubmit={onSubmit}>
<input type="text" onChange={onChange} value={value} ref={inputRef} />
<button>Save</button>
</form>
</div>
</div>
);
};
export default InputModal;
import React from 'react'
const LabelledInput = (props) => {
const { id, label, value, onChange } = props
return (
<div class="labelled--input">
<label for={id}>{label}</label>
<input id={id} onChange={onChange} value={value} />
</div>
)
}
export default LabelledInput
import React from 'react'
const LabelledInput = (props, ref) => {
const { id, label, value, onChange } = props
return (
<div class="labelled--input">
<label for={id}>{label}</label>
<input id={id} onChange={onChange} value={value} ref={ref}/>
</div>
)
}
export default React.forwardRef(LabelledInput)