Skip to content

Commit

Permalink
feat(AsideHeader)!: add ref prop for popup-anchor (#123)
Browse files Browse the repository at this point in the history
* feat(AsideHeader)!: add ref prop for popup-anchor

* chore: add ref to AsideHeader story

* chore: fix lint

* chore: add AsideHeader readme
  • Loading branch information
Lunory authored Oct 5, 2023
1 parent 9836e37 commit de2e3f4
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 12 deletions.
7 changes: 4 additions & 3 deletions src/components/AsideHeader/AsideHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import './AsideHeader.scss';
import {ASIDE_HEADER_COMPACT_WIDTH, ASIDE_HEADER_EXPANDED_WIDTH} from '../constants';
import {useAsideHeaderInnerContextValue} from './useAsideHeaderInnerContextValue';

export const AsideHeader = (props: AsideHeaderProps) => {
export const AsideHeader = React.forwardRef<HTMLDivElement, AsideHeaderProps>((props, ref) => {
const {className, compact} = props;

const size = compact ? ASIDE_HEADER_COMPACT_WIDTH : ASIDE_HEADER_EXPANDED_WIDTH;
const asideHeaderContextValue = useMemo(() => ({size, compact}), [compact, size]);
const asideHeaderInnerContextValue = useAsideHeaderInnerContextValue({...props, size});
Expand All @@ -23,7 +24,7 @@ export const AsideHeader = (props: AsideHeaderProps) => {
<div className={b({compact}, className)}>
<div className={b('pane-container')}>
{/* First Panel */}
<FirstPanel />
<FirstPanel ref={ref} />
{/* Second Panel */}
<Content
size={size}
Expand All @@ -35,4 +36,4 @@ export const AsideHeader = (props: AsideHeaderProps) => {
</AsideHeaderInnerContextProvider>
</AsideHeaderContextProvider>
);
};
});
17 changes: 17 additions & 0 deletions src/components/AsideHeader/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# AsideHeader [⚠️ WIP]

## Imports

```ts
import {AsideHeader} from '@gravity-ui/navigation';
```

## Example

// TODO

## Properties

| Name | Description | Type | Default |
| :--- | :--------------------------- | :----------------------------------------------------: | :-----: |
| ref | `ref` to target popup anchor | `React.ForwardedRef<HTMLDivElement, AsideHeaderProps>` | |
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ body {
}
}

&__settings-ul {
&__settings {
padding-right: 30px;
}

&__settings-ul {
margin: 0;
}

&__panel {
width: 300px;
height: 100%;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const AsideHeaderShowcase: FC<AsideHeaderShowcaseProps> = ({
multipleTooltip = false,
initialCompact = false,
}) => {
const ref = React.useRef<HTMLDivElement>(null);
const [popupVisible, setPopupVisible] = React.useState(false);
const [subheaderPopupVisible, setSubheaderPopupVisible] = React.useState(false);
const [visiblePanel, setVisiblePanel] = React.useState<Panel>();
Expand Down Expand Up @@ -73,6 +74,7 @@ export const AsideHeaderShowcase: FC<AsideHeaderShowcaseProps> = ({
</div>
</Modal>
<AsideHeader
ref={ref}
logo={{
text: 'Service',
icon: logoIcon,
Expand All @@ -93,12 +95,15 @@ export const AsideHeaderShowcase: FC<AsideHeaderShowcaseProps> = ({
setSubheaderPopupVisible(!subheaderPopupVisible);
},
},
popupAnchor: ref,
popupPlacement: ['right-start'],
popupOffset: [10, 10],
popupVisible: subheaderPopupVisible,
onClosePopup: () => setSubheaderPopupVisible(false),
renderPopupContent: () => {
return (
<div className={b('settings-ul')}>
<ul>
<div className={b('settings')}>
<ul className={b('settings-ul')}>
<li>Set 1</li>
<li>Set 2</li>
<li>Set 3</li>
Expand Down
12 changes: 7 additions & 5 deletions src/components/AsideHeader/components/FirstPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useRef} from 'react';
import React, {useRef, useMemo} from 'react';
import {CompositeBar} from '../../CompositeBar/CompositeBar';
import {useAsideHeaderInnerContext} from '../AsideHeaderContext';
import {b} from '../utils';
Expand All @@ -9,7 +9,7 @@ import {Header} from './Header';
import {CollapseButton} from './CollapseButton';
import {Panels} from './Panels';

export const FirstPanel = () => {
export const FirstPanel = React.forwardRef<HTMLDivElement>((_props, ref) => {
const {
size,
onItemClick,
Expand All @@ -23,10 +23,12 @@ export const FirstPanel = () => {

const asideRef = useRef<HTMLDivElement>(null);

const popupAnchorRef = useMemo(() => ref || asideRef, [ref, asideRef]);

return (
<>
<div className={b('aside')} style={{width: size}}>
<div className={b('aside-popup-anchor')} ref={asideRef} />
<div className={b('aside-popup-anchor')} ref={popupAnchorRef} />
<div className={b('aside-content', {['with-decoration']: headerDecoration})}>
<Header />
{visibleMenuItems?.length ? (
Expand All @@ -44,7 +46,7 @@ export const FirstPanel = () => {
{renderFooter?.({
size,
compact: Boolean(compact),
asideRef,
asideRef: popupAnchorRef,
})}
</div>
<CollapseButton />
Expand All @@ -53,4 +55,4 @@ export const FirstPanel = () => {
<Panels />
</>
);
};
});
2 changes: 1 addition & 1 deletion src/components/AsideHeader/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface AsideHeaderGeneralProps {
renderFooter?: (data: {
size: number;
compact: boolean;
asideRef: React.RefObject<HTMLDivElement>;
asideRef: React.ForwardedRef<HTMLDivElement>;
}) => React.ReactNode;
onClosePanel?: () => void;
onChangeCompact?: (compact: boolean) => void;
Expand Down

0 comments on commit de2e3f4

Please sign in to comment.