Skip to content

Instantly share code, notes, and snippets.

View pffigueiredo's full-sized avatar

Pedro Figueiredo pffigueiredo

View GitHub Profile
@pffigueiredo
pffigueiredo / AppStyling.tsx
Created August 9, 2021 17:08
_app file using stylis plugin
import React from 'react';
import { AppProps } from 'next/app';
import { StyleSheetManager } from 'styled-components';
import { StylisPlugin } from 'styled-components';
import stylisRTLPlugin from 'stylis-plugin-rtl';
import { useTranslation } from 'react-i18next';
const App = (props: AppProps): JSX.Element => {
const { Component, pageProps, router } = props;
@pffigueiredo
pffigueiredo / InputLTR.js
Last active May 17, 2021 10:24
Usage of the Left-to-Right mark to enforce LTR behaviour despite the specified direction.
import React, { useState } from "react";
const LEFT_TO_RIGHT_MARK = "‎\u200e"; // marks the input with LTR despite the specified direction
function InputLTR() {
const [cardNumber, setCardNumber] = useState("");
function onInputChange(event) {
const newCardNumber = event.target.value.replace(LEFT_TO_RIGHT_MARK, "");
setCardNumber(newCardNumber);
@pffigueiredo
pffigueiredo / Icon.js
Created May 11, 2021 14:49
RTL Compatible Icon
import React from 'react';
import styled from 'styled-components';
const IconWrapper = styled('div')`
html[dir='rtl'] &.flip-icon {
transform: scaleX(-1);
}
`;
const Icon = ({ name, onClick, noFlip }) => {
@pffigueiredo
pffigueiredo / useOnScreen.ts
Created April 16, 2021 17:20
React hook to check if some element is currently on the view port
import { RefObject, useEffect, useState } from 'react';
import throttle from 'utils/throttle';
export default function useOnScreen(
elementRef: RefObject<HTMLElement>
): boolean {
const [isVisible, setIsVisible] = useState(false);
const onScroll = throttle(() => {
if (!elementRef.current) {
@pffigueiredo
pffigueiredo / useFetchStateMachine.ts
Last active April 27, 2021 10:20
Fetch reducer state machine with typescript support
import { Dispatch, Reducer, useReducer } from 'react';
type Nullable<T> = T | null | undefined;
type FetchMachineState<DataT, ErrorT> = {
status: 'idle' | 'loading' | 'success' | 'failure';
data: Nullable<DataT>;
error: Nullable<ErrorT>;
};