сhange event listeners to hook (#156)
* refactor: micro refactor of useEventListener hook * refactor: changed addEventListener to useEventListener hook * refactor: renamed props type names, TComponent -> Props * refactor: removed customStyles prop from T9n Co-authored-by: mirlan.maksitaliev <mirlan.maksitaliev@instatsport.com>keep-around/af30b88d367751c9e05a735e4a0467a96238ef47
parent
14cbfaf47e
commit
e5e1c032fd
@ -1,11 +1,11 @@ |
||||
import { css } from 'styled-components' |
||||
|
||||
export type TCustomStyles = string | ReturnType<typeof css> |
||||
export type CustomStyles = string | ReturnType<typeof css> |
||||
|
||||
type TCustomStylesMixin = { |
||||
customstyles?: TCustomStyles, |
||||
type CustomStylesMixin = { |
||||
customstyles?: CustomStyles, |
||||
} |
||||
|
||||
export const customStylesMixin = css<TCustomStylesMixin>` |
||||
export const customStylesMixin = css<CustomStylesMixin>` |
||||
${({ customstyles }) => customstyles} |
||||
` |
||||
|
||||
@ -1,30 +1,27 @@ |
||||
import { useEffect, useRef } from 'react' |
||||
import { useRef } from 'react' |
||||
|
||||
type TUseOutsideClickEffect = { |
||||
import { useEventListener } from 'hooks' |
||||
|
||||
type Args = { |
||||
onClick: Function, |
||||
} |
||||
|
||||
export const useOutsideClickEffect = ({ |
||||
onClick, |
||||
}: TUseOutsideClickEffect) => { |
||||
}: Args) => { |
||||
const wrapperRef = useRef<HTMLDivElement>(null) |
||||
useEffect(() => { |
||||
const handleOutsideClick = ({ target }: MouseEvent) => { |
||||
const targetNode = target instanceof Node |
||||
? target |
||||
: null |
||||
const handleOutsideClick = ({ target }: MouseEvent) => { |
||||
const targetNode = target instanceof Node |
||||
? target |
||||
: null |
||||
|
||||
/** деструктуризация wrapperRef не сработает, ссылка на реф теряется */ |
||||
if (!wrapperRef.current?.contains(targetNode)) { |
||||
onClick() |
||||
} |
||||
/** деструктуризация wrapperRef не сработает, ссылка на реф теряется */ |
||||
if (!wrapperRef.current?.contains(targetNode)) { |
||||
onClick() |
||||
} |
||||
} |
||||
|
||||
window.addEventListener('click', handleOutsideClick) |
||||
useEventListener({ callback: handleOutsideClick, event: 'click' }) |
||||
|
||||
return () => { |
||||
window.removeEventListener('click', handleOutsideClick) |
||||
} |
||||
}, [onClick, wrapperRef]) |
||||
return wrapperRef |
||||
} |
||||
|
||||
@ -1,16 +1,16 @@ |
||||
import styled, { css } from 'styled-components/macro' |
||||
|
||||
type TCardLogoStyled = { |
||||
type Props = { |
||||
visa?: boolean, |
||||
} |
||||
|
||||
export const CardLogoStyles = css<TCardLogoStyled>` |
||||
export const CardLogoStyles = css<Props>` |
||||
width: ${({ visa }) => `${visa ? 37 : 30}px`}; |
||||
height: ${({ visa }) => `${visa ? 12 : 19}px`}; |
||||
margin-right: 82px; |
||||
background: ${({ visa }) => `url(/images/${visa ? 'visaLogo.png' : 'masterLogo.png'}) no-repeat`}; |
||||
` |
||||
|
||||
export const VisaLogoWrapper = styled.span<TCardLogoStyled>` |
||||
export const VisaLogoWrapper = styled.span<Props>` |
||||
${CardLogoStyles} |
||||
` |
||||
|
||||
@ -1,5 +1,5 @@ |
||||
import { TCustomTheme } from '../features/Theme/config' |
||||
import { CustomTheme } from '../features/Theme/config' |
||||
|
||||
declare module 'styled-components' { |
||||
export interface DefaultTheme extends TCustomTheme {} |
||||
export interface DefaultTheme extends CustomTheme {} |
||||
} |
||||
|
||||
Loading…
Reference in new issue