Recently, a project requirement is to click the question mark to display the prompt window. The window is fixed, does not scroll with the scroll bar, and can be dragged within the scope. It does not affect the lower form input.
I think of using anti card combined with react draggable. Record the implementation code here:
import React from 'react';
import { Modal, Button, Card, Icon } from 'antd';
import Draggable from 'react-draggable';
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
cardShow: false,
disabled: true,
bounds: { left: 0, top: 0, bottom: 0, right: 0 }, // Initialize drag boundary
};
this.draggleRef = React.createRef();
}
onStart = (event, uiData) => {
const { clientWidth, clientHeight } = window.document.documentElement;
const targetRect = this.draggleRef.current.getBoundingClientRect();
this.setState({
// Set the drag boundary to limit the dragging distance from the top, bottom, left and right of the initial position, the following setting is draggable in the browser window
// If the page has a fixed display header, it must be draggable below the header, you can set top: -targetRect.top + uiData.y + height of the header.
bounds: {
left: -targetRect.left + uiData.x,
right: clientWidth - 300,
top: -targetRect.top + uiData.y,
bottom: clientHeight - (targetRect.bottom - uiData.y),
},
});
};
onClickShow = () => {
this.setState({
cardShow: true,
})
}
onClickClose = () => {
this.setState({
cardShow: false,
bounds: { left: 0, top: 0, bottom: 0, right: 0 },
})
}
render() {
const {disabled, bounds, cardShow} = this.state;
return (
<div style={{width: '100%'}}>
<Draggable
disabled={disabled}
bounds={bounds}
onStart={(event, uiData) => this.onStart(event, uiData)}
>
<div ref={this.draggleRef}>
{cardShow &&
<Card
title={
<div
style={{
width: '100%',
cursor: 'move',
}}
onMouseOver={() => {
this.setState({
disabled: false,
});
}}
onMouseOut={() => {
this.setState({
disabled: true,
});
}}
onFocus={() => {}}
onBlur={() => {}}
>
{'Title'}
</div>
}
extra={
<span onClick={() => this.onClickClose()}>
<Icon type={'close'} style={{fontSize:'16px'}}/>
</span>
}
style={{ width: 300, position: 'fixed', zIndex: 999 }}>
<p>Conetent</p>
</Card>}
</div>
</Draggable>
<span >{'Help'}</span><Icon onClick={() => this.onClickShow()} type="question-circle" />
</div>
);
}
}
Read More:
- [Solved] React Error: ReactDOM.render is no longer supported in React 18.
- [Solved] React Invariant Violation: Minified React error #130
- [Solved] react-router-dom Error: index.js:1 Warning: Functions are not valid as a React child.
- [Solved] React Dependency Error: Invalid tag name “^np.0.2“ of package “react@^np.0.2“: Tags may not have an
- [Solved] React Click the Event to Modify the State Value Error
- React error boundary (What You Should Know & How to Solve)
- React Error: history is undefined [How to Solve]
- [Solved] react-router-dom Error: <NavLink>activeClassName
- React: How to Solve Web3 import error
- [Solved] Error:The above error occurred in one of your React components & A component suspended while respondi
- [Solved] vite2+vue3 jsx Error: React is not defined
- How to Solve React devtools plug-in Error
- [Solved] React Startup Error at the First time :SyntaxError: Unexpected token
- How to React page to achieve entry and exit animation
- [Solved] react-quill failed to install error: Could not resolve dependency
- React project is packaged and set as required Error [How to Solve]
- [Solved] Failed to load config “react-app“ to extend from.
- React native android: How to Upload Formdata
- [Solved] vue Project Error: react lazy eslint error Parsing error: Unexpected token import
- [Solved] react Chrome Browser Error: Uncaught TypeError: Cannot read properties of undefined (reading ‘forEach‘)