| 80 | |
| 81 | // Convenience hook for working with an editable value that is validated via JSON.parse. |
| 82 | export function useEditableValue( |
| 83 | externalValue: any, |
| 84 | ): [UseEditableValueState, UseEditableValueDispatch] { |
| 85 | const [state, dispatch] = useReducer< |
| 86 | UseEditableValueState, |
| 87 | UseEditableValueState, |
| 88 | UseEditableValueAction, |
| 89 | >(useEditableValueReducer, { |
| 90 | editableValue: smartStringify(externalValue), |
| 91 | externalValue, |
| 92 | hasPendingChanges: false, |
| 93 | isValid: true, |
| 94 | parsedValue: externalValue, |
| 95 | }); |
| 96 | if (!Object.is(state.externalValue, externalValue)) { |
| 97 | if (!state.hasPendingChanges) { |
| 98 | dispatch({ |
| 99 | type: 'RESET', |
| 100 | externalValue, |
| 101 | }); |
| 102 | } else { |
| 103 | dispatch({ |
| 104 | type: 'UPDATE', |
| 105 | editableValue: state.editableValue, |
| 106 | externalValue, |
| 107 | }); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return [state, dispatch]; |
| 112 | } |
| 113 | |
| 114 | export function useIsOverflowing( |
| 115 | containerRef: {current: HTMLDivElement | null, ...}, |