Skip to content

Commit

Permalink
added custom hook instead of controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Yasintrm committed Oct 31, 2023
1 parent 36cd8e0 commit bb3f97a
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 62 deletions.
18 changes: 9 additions & 9 deletions lib/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/index.js.map

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions lib/tyk-ui.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/tyk-ui.js.map

Large diffs are not rendered by default.

17 changes: 0 additions & 17 deletions src/components/RevealPanel/RevealPanel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,8 @@ function Component() {
<div className="demo" style={{ position: 'relative' }}>
e and scrambled it to make a type specimen book.
It has survived not only five centuries, but also
the leap into electronic typesetting, remaining
essentially unchanged. It was popularised in the
1960s with the release of Letraset sheets
containing Lorem Ipsum passages,
and more recently with desktop publishing software
like Aldus PageMaker including verse and scrambled it
to make a type specimen book. It has survived not only
five centuries, but also the leap into electronic typesetting,
remaining essentially unchanged. It was popularised in the 1960s
with the release of Letraset sheets containing Lorem Ipsum passages,
and more recently with desktop publishing software like Aldus PageMaker including vers
<RevealPanel height="50%">
Panel content to make a type specimen book.
to make a type specimen book. It has survived not only
five centuries, but also the leap into electronic typesetting,
remaining essentially unchanged. It was popularised in the 1960s
with the release of Letraset sheets containing Lorem Ipsum passages,
and more recently with desktop publishing software like Aldus PageMaker including vers

</RevealPanel>
</div>
);
Expand Down
40 changes: 20 additions & 20 deletions src/components/RevealPanel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import React, {
} from 'react';
import PropTypes from 'prop-types';
import { createPortal } from 'react-dom';
import GetRevealPanelHandler from './js/RevealPanelController';
import { useRevealPanelService } from './js/RevealPanelService';

function RevealPanelHeaderLeft({ children }) {
// eslint-disable-next-line react/jsx-no-useless-fragment
Expand Down Expand Up @@ -46,52 +46,52 @@ const RevealPanel = forwardRef(({
const gutterRef = useRef(null);
const wrapperRef = useRef(null);

const panelControllerRef = useRef(GetRevealPanelHandler());
const panelService = useRevealPanelService();

useEffect(() => {
if (!panelRef.current || !gutterRef.current || !wrapperRef.current) {
return;
}

const controller = panelControllerRef.current;
if (!controller) {
if (!panelService) {
return;
}

controller.setRefs(wrapperRef.current, panelRef.current, gutterRef.current);
controller.on('onHeightChange', onHeightChange);
controller.on('onDragStart', onDragStart);
controller.on('onDragEnd', onDragEnd);
controller.setHeight(height, 'initialize');
panelService.setRefs(wrapperRef.current, panelRef.current, gutterRef.current);
panelService.on('onHeightChange', onHeightChange);
panelService.on('onDragStart', onDragStart);
panelService.on('onDragEnd', onDragEnd);
panelService.setHeight(height, 'initialize');

// eslint-disable-next-line consistent-return
return () => {
controller.unbindEvents();
panelService.unbindEvents();
};
}, []);

useImperativeHandle(
ref,
() => {
const ctl = panelControllerRef.current;
// bind this context
const bind = (func) => func.bind(panelService);

return {
getHeight: ctl.getHeight.bind(ctl),
setHeight: ctl.setHeight.bind(ctl),
hide: ctl.hide.bind(ctl),
show: ctl.show.bind(ctl),
isHidden: ctl.isHidden.bind(ctl),
deinit: ctl.deinit.bind(ctl),
on: ctl.on.bind(ctl),
off: ctl.off.bind(ctl),
getHeight: bind(panelService.getHeight),
setHeight: bind(panelService.setHeight),
hide: bind(panelService.hide),
show: bind(panelService.show),
isHidden: bind(panelService.isHidden),
deinit: bind(panelService.deinit),
on: bind(panelService.on),
off: bind(panelService.off),
};
},
[],
);

let headerLeft = null;
let headerRight = null;
let contents = [];
const contents = [];

React.Children.forEach(children, (child) => {
if (child && React.isValidElement(child)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { useRef } from 'react';

/**
* RevealPanelController is a class that manages the resizing of a panel element.
* RevealPanelService is a class that manages the resizing of a panel element.
* It provides methods to set the panel's height, hide and show it, and attach
* event listeners for resizing.
*/
class RevealPanelController {
class RevealPanelService {
#state = null; // Private state for mouse interactions.

#panel = null; // The panel element to be resized.
Expand Down Expand Up @@ -258,6 +260,7 @@ class RevealPanelController {
}
}

export default function GetRevealPanelHandler() {
return new RevealPanelController();
}
export const useRevealPanelService = () => {
const revealPanelServiceRef = useRef(new RevealPanelService());
return revealPanelServiceRef.current;
};

0 comments on commit bb3f97a

Please sign in to comment.