generated from stacks-network/.github
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
555 additions
and
320 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,7 @@ | ||
import { searchFocusedState } from '@store/search'; | ||
import { useRecoilFocus } from '@common/hooks/use-recoil-focus'; | ||
|
||
export const useSearchFocus = () => { | ||
const results = useRecoilFocus(searchFocusedState); | ||
return [...results]; | ||
}; |
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,67 @@ | ||
import { FocusEvent, HTMLAttributes, MutableRefObject } from 'react'; | ||
|
||
interface FocusWithinProps { | ||
/** Whether the focus within events should be disabled. */ | ||
isDisabled?: boolean; | ||
/** Handler that is called when the target element or a descendant receives focus. */ | ||
onFocusWithin?: (e: FocusEvent) => void; | ||
/** Handler that is called when the target element and all descendants lose focus. */ | ||
onBlurWithin?: (e: FocusEvent) => void; | ||
/** Handler that is called when the the focus within state changes. */ | ||
onFocusWithinChange?: (isFocusWithin: boolean) => void; | ||
ref: MutableRefObject<{ isFocusWithin: boolean }>; | ||
} | ||
|
||
interface FocusWithinResult { | ||
/** Props to spread onto the target element. */ | ||
focusWithinProps: HTMLAttributes<HTMLElement>; | ||
} | ||
|
||
/** | ||
* Handles focus events for the target and its descendants. | ||
*/ | ||
export function useFocusWithin(props: FocusWithinProps): FocusWithinResult { | ||
const state = props.ref.current; | ||
|
||
if (props.isDisabled) { | ||
return { focusWithinProps: {} }; | ||
} | ||
|
||
const onFocus = (e: FocusEvent) => { | ||
if (!state.isFocusWithin) { | ||
if (props.onFocusWithin) { | ||
props.onFocusWithin(e); | ||
} | ||
|
||
if (props.onFocusWithinChange) { | ||
props.onFocusWithinChange(true); | ||
} | ||
|
||
state.isFocusWithin = true; | ||
} | ||
}; | ||
|
||
const onBlur = (e: FocusEvent) => { | ||
// We don't want to trigger onBlurWithin and then immediately onFocusWithin again | ||
// when moving focus inside the element. Only trigger if the currentTarget doesn't | ||
// include the relatedTarget (where focus is moving). | ||
if (state.isFocusWithin && !e.currentTarget.contains(e.relatedTarget as HTMLElement)) { | ||
if (props.onBlurWithin) { | ||
props.onBlurWithin(e); | ||
} | ||
|
||
if (props.onFocusWithinChange) { | ||
props.onFocusWithinChange(false); | ||
} | ||
|
||
state.isFocusWithin = false; | ||
} | ||
}; | ||
|
||
return { | ||
focusWithinProps: { | ||
onFocus: onFocus, | ||
onBlur: onBlur, | ||
}, | ||
}; | ||
} |
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 |
---|---|---|
@@ -1,15 +1,7 @@ | ||
import useSWR from 'swr'; | ||
import { getNetworkMode } from '@common/api/network'; | ||
import { useApiServer } from '@common/hooks/use-api'; | ||
import { useRecoilValue } from 'recoil'; | ||
import { networkModeState } from '@pages/_app'; | ||
|
||
export const useNetworkMode = (initialData?: 'Testnet' | 'Mainnet') => { | ||
const apiServer = useApiServer(); | ||
const { data } = useSWR<'Testnet' | 'Mainnet' | undefined>( | ||
'/v2/info', | ||
() => getNetworkMode(apiServer), | ||
{ | ||
initialData, | ||
} | ||
); | ||
return data; | ||
export const useNetworkMode = () => { | ||
const state = useRecoilValue(networkModeState); | ||
return state; | ||
}; |
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,25 @@ | ||
import React from 'react'; | ||
import { RecoilState, useRecoilState } from 'recoil'; | ||
import { useFocusWithin } from '@common/hooks/use-focus-within'; | ||
|
||
export function useRecoilFocus( | ||
atom: RecoilState<boolean> | ||
): [boolean, any, { removeFocus: () => void }] { | ||
const [isFocused, setIsFocused] = useRecoilState(atom); | ||
|
||
const ref = React.useRef({ | ||
isFocusWithin: false, | ||
}); | ||
|
||
const { focusWithinProps } = useFocusWithin({ | ||
onFocusWithinChange: setIsFocused, | ||
ref, | ||
}); | ||
|
||
const removeFocus = () => { | ||
setIsFocused(false); | ||
ref.current.isFocusWithin = false; | ||
}; | ||
|
||
return [isFocused, focusWithinProps, { removeFocus }]; | ||
} |
Oops, something went wrong.