Skip to content

Instantly share code, notes, and snippets.

View smitroshin's full-sized avatar

Serghei Mitroșin smitroshin

  • Amdaris
  • Chisinau, Moldova
View GitHub Profile
@smitroshin
smitroshin / countdownTimer.html
Created March 17, 2020 09:09
Countdown Timer
<!-- Display the countdown timer in an element -->
<!--Source: https://www.w3schools.com/howto/howto_js_countdown.asp -->
<p id="demo"></p>
<script>
// Set the date we're counting down to
var countDownDate = new Date("Jan 5, 2021 15:37:25").getTime();
// Update the count down every 1 second
var x = setInterval(function() {
@smitroshin
smitroshin / viewportSizes.js
Created April 26, 2020 22:07
Size of browser viewport
const vw = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
const vh = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
@smitroshin
smitroshin / parseJwt.js
Created May 3, 2020 13:20
Decode JWT token
/**
* Decode JWT token
*
* @param {string} token
* @return {object}
*/
const parseJwt = (token) => {
const base64Url = token.split('.')[1];
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
const jsonPayload = decodeURIComponent(
@smitroshin
smitroshin / longPress.js
Created May 15, 2020 14:12
React - long press
class App extends Component {
constructor() {
super()
this.handleButtonPress = this.handleButtonPress.bind(this)
this.handleButtonRelease = this.handleButtonRelease.bind(this)
}
handleButtonPress () {
this.buttonPressTimer = setTimeout(() => alert('long press activated'), 1500);
}
@smitroshin
smitroshin / isInViewport.js
Last active May 24, 2020 13:42
Check is element is visible in viewport
/**
* Source: https://gomakethings.com/how-to-test-if-an-element-is-in-the-viewport-with-vanilla-javascript/
* ! IS BUGGED: In some cases window.innerHeight is bigger than bounding.bottom
* even if element is fully visible.
* TODO: Needs to be improved.
*/
const isInViewport = (elem) => {
const bounding = elem.getBoundingClientRect();
return (
bounding.top >= 0 &&
@smitroshin
smitroshin / debouncedOnChange.js
Created June 22, 2020 12:00
Using throttle and debounce in a React function component
/**
* Source: https://medium.com/@rajeshnaroth/using-throttle-and-debounce-in-a-react-function-component-5489fc3461b3
*/
const Search = () => {
const [userQuery, setUserQuery] = useState("");
const delayedQuery = useCallback(_.debounce(q => sendQuery(q), 500), []);
// alternative
// const delayedQuery = useRef(_.debounce(q => sendQuery(q), 500)).current;
const onChange = e => {
setUserQuery(e.target.value);
@smitroshin
smitroshin / simulateNativeLink.js
Created June 30, 2020 16:08
Simulate native link click programmatically in React.js
@smitroshin
smitroshin / onDrop.js
Created July 6, 2020 16:05
Reading file contents from file input via FileReader
/**
* Source: https://react-dropzone.js.org/
*/
const onDrop = () => (acceptedFiles) => {
acceptedFiles.forEach((file) => {
const reader = new FileReader()
reader.onabort = () => console.log('file reading was aborted')
reader.onerror = () => console.log('file reading has failed')
reader.onload = () => {
@smitroshin
smitroshin / getBase64.js
Created September 23, 2020 12:47
File to base64
/**
* getBase64
* Source: https://ant.design/components/upload/#components-upload-demo-picture-card
*
* @param file
* @returns {Promise<unknown>}
*/
const getBase64 = (file) =>
new Promise((resolve, reject) => {
const reader = new FileReader();
@smitroshin
smitroshin / bootstrapUtilities.scss
Last active September 24, 2023 10:52
Bootstrap utilities - imports
// Bootstrap utilities
@import '~bootstrap/scss/functions';
@import '~bootstrap/scss/variables';
@import '~bootstrap/scss/mixins';
@import '~bootstrap/scss/utilities';
@import '~bootstrap/scss/reboot';
@import '~bootstrap/scss/containers';
@import '~bootstrap/scss/grid';
@import '~bootstrap/scss/helpers';
@import '~bootstrap/scss/utilities/api';