mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-08 06:25:45 +01:00
Adds generic popover component to ui-component
This commit is contained in:
@@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
### Added
|
### Added
|
||||||
|
- Add generic popover component to ui-components
|
||||||
- Show changeset signatures in ui and add public keys ([#1273](https://github.com/scm-manager/scm-manager/pull/1273))
|
- Show changeset signatures in ui and add public keys ([#1273](https://github.com/scm-manager/scm-manager/pull/1273))
|
||||||
|
|
||||||
## [2.3.0] - 2020-07-23
|
## [2.3.0] - 2020-07-23
|
||||||
|
|||||||
@@ -96,6 +96,7 @@ export * from "./navigation";
|
|||||||
export * from "./repos";
|
export * from "./repos";
|
||||||
export * from "./table";
|
export * from "./table";
|
||||||
export * from "./toast";
|
export * from "./toast";
|
||||||
|
export * from "./popover";
|
||||||
|
|
||||||
export {
|
export {
|
||||||
File,
|
File,
|
||||||
|
|||||||
73
scm-ui/ui-components/src/popover/Popover.stories.tsx
Normal file
73
scm-ui/ui-components/src/popover/Popover.stories.tsx
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
/*
|
||||||
|
* MIT License
|
||||||
|
*
|
||||||
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { storiesOf } from "@storybook/react";
|
||||||
|
import React from "react";
|
||||||
|
import styled from "styled-components";
|
||||||
|
import usePopover from "./usePopover";
|
||||||
|
import Popover from "./Popover";
|
||||||
|
|
||||||
|
const Wrapper = styled.div`
|
||||||
|
width: 100%;
|
||||||
|
margin: 20rem;
|
||||||
|
`;
|
||||||
|
|
||||||
|
storiesOf("Popover", module)
|
||||||
|
.addDecorator(storyFn => <Wrapper>{storyFn()}</Wrapper>)
|
||||||
|
.add("Default", () => {
|
||||||
|
const { triggerProps, popoverProps } = usePopover();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<Popover title={<strong>Spaceship Heart of Gold</strong>} width={512} {...popoverProps}>
|
||||||
|
<p>
|
||||||
|
The Heart of Gold is the sleekest, most advanced, coolest spaceship in the galaxy. Its stunning good looks
|
||||||
|
mirror its awesome speed and power. It is powered by the revolutionary new Infinite Improbability Drive,
|
||||||
|
which lets the ship pass through every point in every universe simultaneously.
|
||||||
|
</p>
|
||||||
|
</Popover>
|
||||||
|
<button className="button" {...triggerProps}>
|
||||||
|
Trigger
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})
|
||||||
|
.add("Link", () => {
|
||||||
|
const { triggerProps, popoverProps } = usePopover();
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<Popover title={<strong>Spaceship Heart of Gold</strong>} width={512} {...popoverProps}>
|
||||||
|
<p>
|
||||||
|
The Heart of Gold is the sleekest, most advanced, coolest spaceship in the galaxy. Its stunning good looks
|
||||||
|
mirror its awesome speed and power. It is powered by the revolutionary new Infinite Improbability Drive,
|
||||||
|
which lets the ship pass through every point in every universe simultaneously.
|
||||||
|
</p>
|
||||||
|
</Popover>
|
||||||
|
<a href="#" {...triggerProps}>
|
||||||
|
Trigger
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
129
scm-ui/ui-components/src/popover/Popover.tsx
Normal file
129
scm-ui/ui-components/src/popover/Popover.tsx
Normal file
@@ -0,0 +1,129 @@
|
|||||||
|
/*
|
||||||
|
* MIT License
|
||||||
|
*
|
||||||
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import React, { Dispatch, FC, ReactNode, useLayoutEffect, useRef, useState } from "react";
|
||||||
|
import { Action } from "./usePopover";
|
||||||
|
import styled from "styled-components";
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
title: ReactNode;
|
||||||
|
width?: number;
|
||||||
|
// props should be defined by usePopover
|
||||||
|
offsetTop?: number;
|
||||||
|
offsetLeft?: number;
|
||||||
|
show: boolean;
|
||||||
|
dispatch: Dispatch<Action>;
|
||||||
|
};
|
||||||
|
|
||||||
|
type ContainerProps = {
|
||||||
|
width: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
const PopoverContainer = styled.div<ContainerProps>`
|
||||||
|
position: absolute;
|
||||||
|
z-index: 100;
|
||||||
|
width: ${props => props.width}px;
|
||||||
|
display: block;
|
||||||
|
|
||||||
|
&:before {
|
||||||
|
position: absolute;
|
||||||
|
content: "";
|
||||||
|
border-style: solid;
|
||||||
|
pointer-events: none;
|
||||||
|
height: 0;
|
||||||
|
width: 0;
|
||||||
|
top: 100%;
|
||||||
|
left: ${props => props.width / 2}px;
|
||||||
|
border-color: transparent;
|
||||||
|
border-bottom-color: white;
|
||||||
|
border-left-color: white;
|
||||||
|
border-width: 0.4rem;
|
||||||
|
margin-left: -0.4rem;
|
||||||
|
margin-top: -0.4rem;
|
||||||
|
-webkit-transform-origin: center;
|
||||||
|
transform-origin: center;
|
||||||
|
box-shadow: -1px 1px 2px rgba(10, 10, 10, 0.2);
|
||||||
|
transform: rotate(-45deg);
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
const SmallHr = styled.hr`
|
||||||
|
margin: 0.5em 0;
|
||||||
|
`;
|
||||||
|
|
||||||
|
const PopoverHeading = styled.div`
|
||||||
|
height: 1.5em;
|
||||||
|
`;
|
||||||
|
|
||||||
|
const Popover: FC<Props> = props => {
|
||||||
|
if (!props.show) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return <InnerPopover {...props} />;
|
||||||
|
};
|
||||||
|
|
||||||
|
const InnerPopover: FC<Props> = ({ title, show, width, offsetTop, offsetLeft, dispatch, children }) => {
|
||||||
|
const [height, setHeight] = useState(125);
|
||||||
|
const ref = useRef<HTMLDivElement>(null);
|
||||||
|
useLayoutEffect(() => {
|
||||||
|
if (ref.current) {
|
||||||
|
setHeight(ref.current.clientHeight);
|
||||||
|
}
|
||||||
|
}, [ref]);
|
||||||
|
|
||||||
|
const onMouseEnter = () => {
|
||||||
|
dispatch({
|
||||||
|
type: "enter-popover"
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const onMouseLeave = () => {
|
||||||
|
dispatch({
|
||||||
|
type: "leave-popover"
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const top = (offsetTop || 0) - height - 5;
|
||||||
|
const left = (offsetLeft || 0) - width! / 2;
|
||||||
|
return (
|
||||||
|
<PopoverContainer
|
||||||
|
onMouseEnter={onMouseEnter}
|
||||||
|
onMouseLeave={onMouseLeave}
|
||||||
|
className={"box"}
|
||||||
|
style={{ top: `${top}px`, left: `${left}px` }}
|
||||||
|
width={width!}
|
||||||
|
ref={ref}
|
||||||
|
>
|
||||||
|
<PopoverHeading>{title}</PopoverHeading>
|
||||||
|
<SmallHr />
|
||||||
|
{children}
|
||||||
|
</PopoverContainer>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
Popover.defaultProps = {
|
||||||
|
width: 120
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Popover;
|
||||||
26
scm-ui/ui-components/src/popover/index.ts
Normal file
26
scm-ui/ui-components/src/popover/index.ts
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
/*
|
||||||
|
* MIT License
|
||||||
|
*
|
||||||
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
export { default as Popover } from "./Popover";
|
||||||
|
export { default as usePopover } from "./usePopover";
|
||||||
137
scm-ui/ui-components/src/popover/usePopover.ts
Normal file
137
scm-ui/ui-components/src/popover/usePopover.ts
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
/*
|
||||||
|
* MIT License
|
||||||
|
*
|
||||||
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { Dispatch, useReducer, useRef } from "react";
|
||||||
|
|
||||||
|
type EnterTrigger = {
|
||||||
|
type: "enter-trigger";
|
||||||
|
offsetTop: number;
|
||||||
|
offsetLeft: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
type LeaveTrigger = {
|
||||||
|
type: "leave-trigger";
|
||||||
|
};
|
||||||
|
|
||||||
|
type EnterPopover = {
|
||||||
|
type: "enter-popover";
|
||||||
|
};
|
||||||
|
|
||||||
|
type LeavePopover = {
|
||||||
|
type: "leave-popover";
|
||||||
|
};
|
||||||
|
|
||||||
|
export type Action = EnterTrigger | LeaveTrigger | EnterPopover | LeavePopover;
|
||||||
|
|
||||||
|
type State = {
|
||||||
|
offsetTop?: number;
|
||||||
|
offsetLeft?: number;
|
||||||
|
onPopover: boolean;
|
||||||
|
onTrigger: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
const initialState = {
|
||||||
|
onPopover: false,
|
||||||
|
onTrigger: false
|
||||||
|
};
|
||||||
|
|
||||||
|
const reducer = (state: State, action: Action): State => {
|
||||||
|
switch (action.type) {
|
||||||
|
case "enter-trigger": {
|
||||||
|
if (state.onPopover) {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
offsetTop: action.offsetTop,
|
||||||
|
offsetLeft: action.offsetLeft,
|
||||||
|
onTrigger: true,
|
||||||
|
onPopover: false
|
||||||
|
};
|
||||||
|
}
|
||||||
|
case "leave-trigger": {
|
||||||
|
if (state.onPopover) {
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
onTrigger: false
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return initialState;
|
||||||
|
}
|
||||||
|
case "enter-popover": {
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
onPopover: true
|
||||||
|
};
|
||||||
|
}
|
||||||
|
case "leave-popover": {
|
||||||
|
if (state.onTrigger) {
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
onPopover: false
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return initialState;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const dispatchDeferred = (dispatch: Dispatch<Action>, action: Action) => {
|
||||||
|
setTimeout(() => dispatch(action), 250);
|
||||||
|
};
|
||||||
|
|
||||||
|
const usePopover = () => {
|
||||||
|
const [state, dispatch] = useReducer(reducer, initialState);
|
||||||
|
const triggerRef = useRef<HTMLElement | null>(null);
|
||||||
|
|
||||||
|
const onMouseOver = () => {
|
||||||
|
const current = triggerRef.current!;
|
||||||
|
dispatchDeferred(dispatch, {
|
||||||
|
type: "enter-trigger",
|
||||||
|
offsetTop: current.offsetTop,
|
||||||
|
offsetLeft: current.offsetLeft + current.offsetWidth / 2
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const onMouseLeave = () => {
|
||||||
|
dispatchDeferred(dispatch, {
|
||||||
|
type: "leave-trigger"
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
triggerProps: {
|
||||||
|
onMouseOver,
|
||||||
|
onMouseLeave,
|
||||||
|
ref: (node: HTMLElement | null) => (triggerRef.current = node)
|
||||||
|
},
|
||||||
|
popoverProps: {
|
||||||
|
dispatch,
|
||||||
|
show: state.onPopover || state.onTrigger,
|
||||||
|
offsetTop: state.offsetTop,
|
||||||
|
offsetLeft: state.offsetLeft
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export default usePopover;
|
||||||
Reference in New Issue
Block a user