Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2173 Font size drop down does not collapse #2271

Merged
merged 1 commit into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/ketcher-react/src/hooks/useClickOutside.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useEffect, RefObject } from 'react'

export const useClickOutside = (
targetRef: RefObject<Node>,
callback: () => void
): void => {
useEffect(() => {
document.addEventListener('click', onClickOutside)
return () => {
document.removeEventListener('click', onClickOutside)
}
}, [])

const onClickOutside = (e: Event) => {
if (!targetRef.current?.contains(e.target as Node)) {
callback()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
* limitations under the License.
***************************************************************************/

import { useEffect, useMemo, useState } from 'react'
import { useEffect, useMemo, useState, useRef } from 'react'
import { useClickOutside } from '../../../../../../../hooks/useClickOutside'

import classes from './FontControl.module.less'

Expand All @@ -24,6 +25,10 @@ export const FontControl = ({ editorState, setEditorState, styles }) => {
const defaultFontSize = '13px'
const [isShowingFontSizeMenu, setIsShowingFontSizeMenu] = useState(false)
const [currentFontSize, setCurrentFontSize] = useState(defaultFontSize)
const wrapperRef = useRef<HTMLDivElement>(null)
const onClickOutsideCloseDrowndown = (): void =>
setIsShowingFontSizeMenu(false)
useClickOutside(wrapperRef, onClickOutsideCloseDrowndown)

const setFontSize = (e, value) => {
e.preventDefault()
Expand Down Expand Up @@ -58,7 +63,7 @@ export const FontControl = ({ editorState, setEditorState, styles }) => {
)

return (
<div>
<div ref={wrapperRef}>
<button
className={classes.fontBtn}
onMouseDown={(e) => {
Expand Down