forked from cybersemics/em
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix/blur shrink ondelete (cybersemics#2596)
Co-authored-by: Raine Revere <[email protected]>
- Loading branch information
Showing
5 changed files
with
81 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React, { useEffect, useRef } from 'react' | ||
import { cx } from '../../styled-system/css' | ||
import { editableRecipe } from '../../styled-system/recipes' | ||
import Thought from '../@types/Thought' | ||
|
||
// Create a regular expression to match and remove BEM modifier (variant) classes | ||
const editableClass = cx( | ||
editableRecipe({ | ||
preventAutoscroll: true, | ||
}), | ||
).split(' ') | ||
|
||
// Filter classes containing '--' to identify variant classes | ||
const modifiers = editableClass.filter(cls => cls.includes('--')) | ||
|
||
// Precompile the regex once, so it's not recreated on each render | ||
const regexPreventAutoscroll = new RegExp(`\\b(${modifiers.join('|')})\\b`, 'g') | ||
|
||
/** | ||
* Custom hook to capture and cache the static HTML string of a node. Useful for animating a thought after it has been deleted. Removes preventAutoscroll classes to avoid accidentally rendering at 0% opacity. | ||
* | ||
* @param thought - The thought object. | ||
* @param elementRef - The ref of the Thought container element. | ||
* @returns The cached HTML string. | ||
*/ | ||
const useCachedThoughtHtml = ({ | ||
thought, | ||
elementRef, | ||
}: { | ||
thought: Thought | ||
elementRef: React.RefObject<HTMLDivElement> | ||
}) => { | ||
// Cache the DOM before it is deleted | ||
const cachedHTMLRef = useRef<string | null>(null) | ||
|
||
// Capture the static innerHTML of the thought container whenever the thought changes | ||
useEffect(() => { | ||
if (thought && elementRef.current) { | ||
cachedHTMLRef.current = elementRef.current.innerHTML.replace(regexPreventAutoscroll, '').trim() | ||
} | ||
}, [thought, elementRef]) | ||
|
||
return cachedHTMLRef | ||
} | ||
|
||
export default useCachedThoughtHtml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters