Skip to content

Commit

Permalink
feat: option to unselect layer when click infobox close (#564)
Browse files Browse the repository at this point in the history
* feat: add property hideOnClose

* refactor: unselectOnClose

* refactor: use layer select

* feat: avoid ui change during exit animation
  • Loading branch information
airslice committed Mar 22, 2023
1 parent f74b561 commit f2b2f26
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/core/Crust/Infobox/Frame/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type Props = {
size?: "small" | "medium" | "large";
position?: "right" | "middle" | "left";
visible?: boolean;
unselectOnClose?: boolean;
noContent?: boolean;
useMask?: boolean;
typography?: Typography;
Expand All @@ -39,6 +40,7 @@ export type Props = {
onEntered?: () => void;
onExit?: () => void;
onExited?: () => void;
onClose?: () => void;
};

const Frame: React.FC<Props> = ({
Expand All @@ -53,6 +55,7 @@ const Frame: React.FC<Props> = ({
outlineColor,
outlineWidth,
visible,
unselectOnClose,
noContent,
useMask,
typography,
Expand All @@ -70,6 +73,7 @@ const Frame: React.FC<Props> = ({
onEntered,
onExit,
onExited,
onClose,
}) => {
const isSmallWindow = useMedia("(max-width: 624px)");
const ref = useRef<HTMLDivElement>(null);
Expand All @@ -84,8 +88,11 @@ const Frame: React.FC<Props> = ({
}, [open, noContent, isSmallWindow]);

const handleClose = useCallback(() => {
setOpen(false);
}, []);
if (!unselectOnClose) {
setOpen(false);
}
onClose?.();
}, [onClose, unselectOnClose]);

useEffect(() => {
if (!ref2.current) return;
Expand Down
6 changes: 5 additions & 1 deletion src/core/Crust/Infobox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export type Props = {
onBlockInsert?: (bi: number, i: number, pos?: "top" | "bottom") => void;
renderBlock?: (block: BlockProps) => ReactNode;
renderInsertionPopup?: (onSelect: (bi: number) => void, onClose: () => void) => ReactNode;
onClose?: () => void;
};

const Infobox: React.FC<Props> = ({
Expand All @@ -61,6 +62,7 @@ const Infobox: React.FC<Props> = ({
onBlockMove,
onBlockInsert,
renderInsertionPopup,
onClose,
...props
}) => {
const {
Expand Down Expand Up @@ -88,6 +90,7 @@ const Infobox: React.FC<Props> = ({
useMask={!!property?.useMask}
outlineWidth={property?.outlineWidth}
visible={visible}
unselectOnClose={property?.unselectOnClose}
noContent={!blocks?.length}
theme={infoboxTheme}
backgroundColor={property?.bgcolor}
Expand All @@ -101,7 +104,8 @@ const Infobox: React.FC<Props> = ({
onClick={() => selectedBlockId && onBlockSelect?.(undefined)}
onEnter={setNotReadyToRender}
onEntered={setReadyToRender}
onExit={setNotReadyToRender}>
onExit={setNotReadyToRender}
onClose={onClose}>
{blocks?.map((b, i) => (
<Field
key={b.id}
Expand Down
1 change: 1 addition & 0 deletions src/core/Crust/Infobox/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type InfoboxProperty = {
outlineWidth?: number;
useMask?: boolean;
defaultContent?: "description" | "attributes";
unselectOnClose?: boolean;
};

export type Block<P = any> = {
Expand Down
3 changes: 3 additions & 0 deletions src/core/Crust/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export type Props = {
onWidgetAreaSelect?: (widgetArea?: WidgetAreaType) => void;
// infobox events
onInfoboxMaskClick?: () => void;
onInfoboxClose?: () => void;
onBlockSelect?: (id?: string) => void;
onBlockChange?: <T extends ValueType>(
blockId: string,
Expand Down Expand Up @@ -141,6 +142,7 @@ export default function Crust({
onWidgetAlignmentUpdate,
onWidgetAreaSelect,
onInfoboxMaskClick,
onInfoboxClose,
onBlockSelect,
onBlockChange,
onBlockMove,
Expand Down Expand Up @@ -225,6 +227,7 @@ export default function Crust({
onBlockInsert={onBlockInsert}
renderBlock={renderBlock}
renderInsertionPopup={renderInfoboxInsertionPopup}
onClose={onInfoboxClose}
/>
</Plugins>
);
Expand Down
6 changes: 6 additions & 0 deletions src/core/Visualizer/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ export default function useHooks({
: undefined,
[selectedLayer, defaultInfobox, blocks],
);
const handleInfoboxClose = useCallback(() => {
if (infobox?.property?.unselectOnClose) {
mapRef?.current?.layers.select(undefined);
}
}, [infobox]);

// scene
const [overriddenSceneProperty, overrideSceneProperty] = useOverriddenProperty(sceneProperty);
Expand Down Expand Up @@ -264,6 +269,7 @@ export default function useHooks({
overrideSceneProperty,
handleLayerEdit,
onLayerEdit,
handleInfoboxClose,
};
}

Expand Down
2 changes: 2 additions & 0 deletions src/core/Visualizer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ export default function Visualizer({
overrideSceneProperty,
handleLayerEdit,
onLayerEdit,
handleInfoboxClose,
} = useHooks({
rootLayerId,
isEditable,
Expand Down Expand Up @@ -227,6 +228,7 @@ export default function Visualizer({
onWidgetAlignmentUpdate={onWidgetAlignmentUpdate}
onWidgetAreaSelect={onWidgetAreaSelect}
onInfoboxMaskClick={onInfoboxMaskClick}
onInfoboxClose={handleInfoboxClose}
onBlockSelect={handleBlockSelect}
onBlockChange={onBlockChange}
onBlockMove={onBlockMove}
Expand Down
1 change: 1 addition & 0 deletions src/core/mantle/compat/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export type InfoboxProperty = {
outlineWidth?: number;
useMask?: boolean;
defaultContent?: "description" | "attributes";
unselectOnClose?: boolean;
};
};

Expand Down

0 comments on commit f2b2f26

Please sign in to comment.