Skip to content

Instantly share code, notes, and snippets.

View nithiwatter's full-sized avatar
😭
Frieren TT

Ter nithiwatter

😭
Frieren TT
View GitHub Profile
@nithiwatter
nithiwatter / caret.js
Created June 18, 2022 15:11 — forked from nothingismagick/caret.js
Small script to detect caret pixel position in contenteditable div
/**
* Get the caret position in all cases
*
* @returns {object} left, top distance in pixels
*/
getCaretTopPoint () {
const sel = document.getSelection()
const r = sel.getRangeAt(0)
let rect
let r2
import React from 'react'
export function makeLoadable({ loading, render, loader }) {
const Comp = render
? React.lazy(() =>
loader().then(loaded => ({
...loaded,
default: props => render(loaded, props),
}))
)
@nithiwatter
nithiwatter / useMousePosition.ts
Created April 27, 2022 05:10 — forked from eldh/useMousePosition.ts
useMousePosition
import * as React from "react";
import { throttle } from "lodash";
/**
* Mouse position as a tuple of [x, y]
*/
type MousePosition = [number, number];
/**
* Hook to get the current mouse position
import * as React from "react";
import { useMousePosition } from "~/hooks/useMousePosition";
/** Component to cover the area between the mouse cursor and the sub-menu, to allow moving cursor to lower parts of sub-menu without the sub-menu disappearing. */
export function MouseSafeArea(props: { parentRef: React.RefObject<HTMLDivElement> }) {
const { x = 0, y = 0, height: h = 0, width: w = 0 } = props.parentRef.current?.getBoundingClientRect() || {};
const [mouseX, mouseY] = useMousePosition();
const positions = { x, y, h, w, mouseX, mouseY };
return (
<div