css 制作一个类似CodeSandbox控制台的可拖动拆分面板

r3i60tvu  于 2022-12-27  发布在  其他
关注(0)|答案(2)|浏览(163)

我想为编辑器做一个可拖动的拆分面板,其行为主要类似于CodeSandbox的Console面板:
1.当我们单击Console时,面板展开,箭头变为ArrowDown,表示关闭。
1.面板的边框是可拖动的。
1.当我们单击展开面板上的Console时,面板关闭,箭头变为ArrowUp以展开。
我有下面的代码(https://codesandbox.io/s/reset-forked-ivebxf?file=/src/App.js)由https://github.com/johnwalley/allotment。它可以展开和关闭面板,但有一个冲突的大小。当面板关闭,我们拖动边界,我希望箭头成为ArrowDown。同样,当面板打开,我们拖动边界向下到底部,我希望箭头成为ArrowUp
有人知道怎么做吗?我也对其他react库持开放态度。

import React from "react";
import { Allotment } from "allotment";
import "allotment/dist/style.css";
import styles from "./App.module.css";

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      toExpand: true
    };
  }

  render() {
    return (
      <div>
        <div className={styles.container}>
          <Allotment vertical>
            <Allotment.Pane>abc</Allotment.Pane>
            {!this.state.toExpand && (
              <Allotment.Pane preferredSize="50%">
                <div
                  onClick={() => {
                    this.setState({ toExpand: !this.state.toExpand });
                  }}
                >
                  Console &nbsp;
                  {this.state.toExpand ? "ArrowUp" : "ArrowDown"}
                </div>
              </Allotment.Pane>
            )}
            {this.state.toExpand && (
              <Allotment.Pane preferredSize="0%">
                <div
                  onClick={() => {
                    this.setState({ toExpand: !this.state.toExpand });
                  }}
                >
                  Console &nbsp;
                  {this.state.toExpand ? "ArrowUp" : "ArrowDown"}
                </div>
              </Allotment.Pane>
            )}
          </Allotment>
        </div>
      </div>
    );
  }
}
7qhs6swi

7qhs6swi1#

可以使用【下拨】组件的onChange回调函数,我的解决方法是:

export default class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      toExpand: true
    };
    this.myRef = React.createRef();
  }

  handleChange = (sizes) => {
    if (sizes.length > 1) {
      if (sizes[1] < 31) {
        this.setState({ toExpand: true });
      } else {
        this.setState({ toExpand: false });
      }
    }
  };

  render() {
    return (
      <div>
        <div className={styles.container}>
          <Allotment vertical onChange={this.handleChange} ref={this.myRef}>
            <Allotment.Pane>Main Area</Allotment.Pane>
            <Allotment.Pane preferredSize="0%">
              <div
                onClick={() => {
                  if (this.state.toExpand) {
                    this.myRef.current.resize([50, 50]);
                  } else {
                    this.myRef.current.resize([10000, 0]);
                  }
                }}
              >
                Console &nbsp;
                {this.state.toExpand ? "ArrowUp" : "ArrowDown"}
              </div>
            </Allotment.Pane>
          </Allotment>
        </div>
      </div>
    );
  }
}
4smxwvx5

4smxwvx52#

<Allotment onChange={}/>组件有一个onChange事件来标识每一个变化,你必须做一个触发点(我的例子是400,或者你可以根据窗口高度来计算),并且改变Arrow Mark的值

<Allotment vertical onChange={this.changeSize}>

changeSize = (e) => {
    if (e[0] > 400) { // use your desired trigger
      console.log(e[0]);
      this.setState({ toExpand: !this.state.toExpand });
    }
  };

相关问题