MCPcopy Create free account
hub / github.com/dailydotdev/apps / ShellField

Function ShellField

packages/shared/src/components/shell/ShellField.tsx:35–205  ·  view source on GitHub ↗
({
  placeholder,
  onOpen,
  value = '',
  onChange,
  onSubmit,
  onFocus,
  overModal = false,
}: ShellFieldProps)

Source from the content-addressed store, hash-verified

33 .join(', ');
34
35export function ShellField({
36 placeholder,
37 onOpen,
38 value = '',
39 onChange,
40 onSubmit,
41 onFocus,
42 overModal = false,
43}: ShellFieldProps): ReactElement | null {
44 const isPhone = useIsPhone();
45 // Sideways the bar stays compact, so the field stands on its compact top.
46 const barHeight = useIsPhoneLandscape() ? cluster.compact : cluster.rest;
47 const router = useRouter();
48 // Settings and forms have no bar; the field rests in the bar's place.
49 const hasBar =
50 !overModal && !hidesCluster(router?.pathname ?? '', router?.asPath);
51 const { p } = useShellScroll();
52 const [isFocused, setIsFocused] = useState(false);
53 const inputRef = useRef<HTMLInputElement>(null);
54 // undefined until the anchor has looked for its modal's overlay; null when
55 // there is none, and the field falls back to the root.
56 const [modalOverlay, setModalOverlay] = useState<Element | null>();
57 const anchorRef = useCallback((node: HTMLSpanElement | null) => {
58 if (node) {
59 setModalOverlay(node.closest('.ReactModal__Overlay'));
60 }
61 }, []);
62 const viewport = useVisualViewport(isFocused);
63 // The app footer owns the bottom while it is up.
64 const { title: appFooterTitle } = useMobileAppFooterContext();
65 const hasAppFooter = !!appFooterTitle;
66 const isShown = isPhone && !hasAppFooter;
67 useRegisterShellField(isShown);
68
69 useEffect(() => {
70 setShellFieldFocused(isFocused);
71 return () => setShellFieldFocused(false);
72 }, [isFocused]);
73
74 if (!isShown) {
75 return null;
76 }
77
78 // iOS lays the keyboard over the page instead of resizing it, so the
79 // visual viewport is the only place that knows how tall the keyboard is.
80 const keyboard = isFocused
81 ? Math.max(
82 0,
83 window.innerHeight - (viewport.height ?? 0) - (viewport.offsetTop ?? 0),
84 )
85 : 0;
86 const progress = isFocused ? 0 : p;
87 const restBottom = `calc(${cluster.floor} + ${
88 hasBar ? barHeight + field.gap : 0
89 }px)`;
90 const focusedBottom =
91 keyboard > 0
92 ? `${keyboard + field.gap}px`

Callers

nothing calls this directly

Calls 12

useIsPhoneFunction · 0.90
useIsPhoneLandscapeFunction · 0.90
hidesClusterFunction · 0.90
useShellScrollFunction · 0.90
useVisualViewportFunction · 0.90
useRegisterShellFieldFunction · 0.90
setShellFieldFocusedFunction · 0.90
lerpFunction · 0.90
revealShellFunction · 0.90
onFocusFunction · 0.70
useRouterFunction · 0.50
onChangeFunction · 0.50

Tested by

no test coverage detected