-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add anchors to dashboard #1698
base: main
Are you sure you want to change the base?
Add anchors to dashboard #1698
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,53 @@ | ||
.dashkit-plugin-container { | ||
&__anchor { | ||
opacity: 0; | ||
right: 100%; | ||
padding-right: 4px; | ||
height: 100%; | ||
float: left; | ||
|
||
&_visible, | ||
&:hover, | ||
&:focus-visible { | ||
opacity: 1; | ||
} | ||
|
||
&_absolute { | ||
position: absolute; | ||
} | ||
|
||
&_size_l { | ||
padding-top: 5px; | ||
font-size: 24px; | ||
line-height: 28px; | ||
} | ||
|
||
&_size_m { | ||
padding-top: 7px; | ||
font-size: 20px; | ||
line-height: 24px; | ||
} | ||
|
||
&_size_s { | ||
padding-top: 7px; | ||
font-size: var(--g-text-body-3-font-size); | ||
line-height: 24px; | ||
} | ||
|
||
&_size_xs { | ||
padding-top: 9px; | ||
font-size: 15px; | ||
line-height: 20px; | ||
} | ||
} | ||
|
||
&__wrapper { | ||
height: 100%; | ||
|
||
&:hover .dashkit-plugin-container__anchor { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Class doesn't work because anchor is placed outside the |
||
opacity: 1; | ||
} | ||
|
||
&_widget { | ||
width: 100%; | ||
} | ||
|
@@ -11,7 +57,8 @@ | |
} | ||
|
||
&_title { | ||
overflow: hidden; | ||
scroll-margin-left: calc(24px + var(--gn-aside-header-size, 0px)); | ||
overflow: clip; | ||
display: flex; | ||
align-items: center; | ||
max-height: 100%; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import React from 'react'; | ||
|
||
import type {PluginTitleProps} from '@gravity-ui/dashkit'; | ||
import {Link} from '@gravity-ui/uikit'; | ||
import block from 'bem-cn-lite'; | ||
import {useHistory, useLocation} from 'react-router'; | ||
|
||
const b = block('dashkit-plugin-container'); | ||
|
||
interface DashAnchorLinkProps { | ||
size: PluginTitleProps['data']['size']; | ||
x: number; | ||
to: string; | ||
} | ||
|
||
const DashAnchorLink = ({size, x, to}: DashAnchorLinkProps) => { | ||
const location = useLocation(); | ||
const history = useHistory(); | ||
const hash = `#${encodeURIComponent(to)}`; | ||
const isLinkVisible = false || location.hash === hash; | ||
|
||
return ( | ||
<Link | ||
onClick={(e) => { | ||
e.preventDefault(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is a link, then it should open in a new tab by cmd + click (the table of contents does so) |
||
history.push({...location, hash}); | ||
}} | ||
className={b('anchor', { | ||
size, | ||
absolute: x === 0, | ||
visible: isLinkVisible, | ||
})} | ||
href={hash} | ||
> | ||
# | ||
</Link> | ||
); | ||
}; | ||
|
||
export default DashAnchorLink; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import React from 'react'; | ||
|
||
export const useUpdateEffect = (effect: React.EffectCallback, deps: React.DependencyList) => { | ||
const isInitialMount = React.useRef(true); | ||
|
||
React.useEffect(() => { | ||
if (isInitialMount.current) { | ||
isInitialMount.current = false; | ||
} else { | ||
return effect(); | ||
} | ||
}, deps); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ body, | |
margin: 0; | ||
padding: 0; | ||
box-sizing: border-box; | ||
scroll-behavior: smooth; | ||
} | ||
|
||
html, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,7 +65,7 @@ import {getIsAsideHeaderEnabled} from '../../../../components/AsideHeaderAdapter | |
import {getConfiguredDashKit} from '../../../../components/DashKit/DashKit'; | ||
import {DL} from '../../../../constants'; | ||
import type SDK from '../../../../libs/sdk'; | ||
import Utils from '../../../../utils'; | ||
import Utils, {scrollToHash} from '../../../../utils'; | ||
import {TYPES_TO_DIALOGS_MAP, getActionPanelItems} from '../../../../utils/getActionPanelItems'; | ||
import {EmptyState} from '../../components/EmptyState/EmptyState'; | ||
import Loader from '../../components/Loader/Loader'; | ||
|
@@ -152,6 +152,7 @@ type DashBodyState = { | |
loaded: boolean; | ||
prevMeta: {tabId: string | null; entryId: string | null}; | ||
loadedItemsMap: Map<string, boolean>; | ||
hash: string; | ||
}; | ||
|
||
type BodyProps = StateProps & DispatchProps & RouteComponentProps & OwnProps; | ||
|
@@ -180,18 +181,27 @@ const GROUPS_WEIGHT = { | |
// Body is used as a core in different environments | ||
class Body extends React.PureComponent<BodyProps> { | ||
static getDerivedStateFromProps(props: BodyProps, state: DashBodyState) { | ||
let newState: Partial<DashBodyState> = {}; | ||
|
||
const { | ||
prevMeta: {entryId, tabId}, | ||
} = state; | ||
|
||
// reset loaded before new tab/entry items are mounted | ||
if (props.entryId !== entryId || props.tabId !== tabId) { | ||
state.loadedItemsMap.clear(); | ||
newState = { | ||
prevMeta: {tabId: props.tabId, entryId: props.entryId}, | ||
loaded: false, | ||
}; | ||
} | ||
|
||
return {prevMeta: {tabId: props.tabId, entryId: props.entryId}, loaded: false}; | ||
const newHash = props.location.hash; | ||
if (newHash !== state.hash) { | ||
newState.hash = newHash; | ||
scrollToHash({hash: newHash.replace('#', ''), withDelay: props.tabId !== tabId}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You do |
||
} | ||
|
||
return null; | ||
return Object.keys(newState).length ? newState : null; | ||
} | ||
|
||
dashKitRef = React.createRef<DashKitComponent>(); | ||
|
@@ -250,6 +260,7 @@ class Body extends React.PureComponent<BodyProps> { | |
prevMeta: {tabId: null, entryId: null}, | ||
loaded: false, | ||
loadedItemsMap: new Map<string, boolean>(), | ||
hash: '', | ||
}; | ||
|
||
groups: DashKitGroup[] = [ | ||
|
@@ -289,6 +300,8 @@ class Body extends React.PureComponent<BodyProps> { | |
// if localStorage already have a dash item, we need to set it to state | ||
this.storageHandler(); | ||
|
||
scrollToHash({hash: this.props.location.hash, withDelay: true, checkUserScroll: true}); | ||
|
||
window.addEventListener('storage', this.storageHandler); | ||
} | ||
|
||
|
@@ -853,14 +866,16 @@ class Body extends React.PureComponent<BodyProps> { | |
isEditModeLoading, | ||
globalParams, | ||
dashkitSettings, | ||
disableHashNavigation, | ||
} = this.props; | ||
|
||
const tabDataConfig = DL.IS_MOBILE | ||
? this.getMobileLayout() | ||
: (tabData as DashKitProps['config'] | null); | ||
|
||
const isEmptyTab = !tabDataConfig?.items.length; | ||
const DashKit = getConfiguredDashKit(); | ||
|
||
const DashKit = getConfiguredDashKit(undefined, disableHashNavigation); | ||
|
||
return isEmptyTab && !isGlobalDragging ? ( | ||
<EmptyState | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,13 +11,13 @@ import {useDispatch} from 'react-redux'; | |
import {Link, useLocation} from 'react-router-dom'; | ||
import {TableOfContentQa} from 'shared'; | ||
import {DL} from 'ui/constants'; | ||
import {scrollToHash} from 'ui/utils'; | ||
import { | ||
selectHashStates, | ||
selectShowTableOfContent, | ||
selectTabId, | ||
selectTabs, | ||
} from 'units/dash/store/selectors/dashTypedSelectors'; | ||
import {scrollIntoView} from 'utils'; | ||
|
||
import { | ||
appendSearchQuery, | ||
|
@@ -33,9 +33,7 @@ const i18n = I18n.keyset('dash.table-of-content.view'); | |
|
||
const b = block('table-of-content'); | ||
|
||
const scrollIntoViewOptions: ScrollIntoViewOptions = {behavior: 'smooth'}; | ||
const dispatchResizeTimeout = 200; | ||
const scrollDelay = 300; | ||
|
||
const getHash = ({ | ||
itemTitle, | ||
|
@@ -53,16 +51,6 @@ const getHash = ({ | |
return itemTitle ? `#${encodeURIComponent(itemTitle)}` : ''; | ||
}; | ||
|
||
const scrollIntoViewWithTimeout = (itemId: string) => { | ||
setTimeout( | ||
() => scrollIntoView(itemId, scrollIntoViewOptions), | ||
// to have time to change the height of the react-grid-layout (200ms) | ||
// DashKit rendering ended after location change (with manual page refresh) (50-70ms) | ||
// small margin | ||
scrollDelay, | ||
); | ||
}; | ||
|
||
const TableOfContent: React.FC<{disableHashNavigation?: boolean}> = React.memo( | ||
({disableHashNavigation}) => { | ||
const dispatch = useDispatch(); | ||
|
@@ -98,7 +86,7 @@ const TableOfContent: React.FC<{disableHashNavigation?: boolean}> = React.memo( | |
handleToggleTableOfContent(); | ||
} | ||
if (disableHashNavigation) { | ||
scrollIntoViewWithTimeout(encodeURIComponent(itemTitle)); | ||
scrollToHash({hash: `#${encodeURIComponent(itemTitle)}`, withDelay: true}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding '#' is not necessary, is it? |
||
} | ||
}, | ||
[isSelectedTab, disableHashNavigation, dispatch, handleToggleTableOfContent], | ||
|
@@ -125,11 +113,6 @@ const TableOfContent: React.FC<{disableHashNavigation?: boolean}> = React.memo( | |
[disableHashNavigation, hashStates, isSelectedTab, location], | ||
); | ||
|
||
React.useEffect(() => { | ||
if (location.hash && !disableHashNavigation) { | ||
scrollIntoViewWithTimeout(location.hash.replace('#', '')); | ||
} | ||
}, [location.hash, disableHashNavigation]); | ||
React.useEffect(() => { | ||
// to recalculate ReactGridLayout | ||
dispatchResize(dispatchResizeTimeout); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use object for props here
upd: Maybe only for second argument (I saw that it breaks compatibility)