项目作者: mizchi

项目描述 :
Editable layout system
高级语言: TypeScript
项目地址: git://github.com/mizchi/react-unite.git
创建时间: 2018-11-13T22:57:42Z
项目社区:https://github.com/mizchi/react-unite

开源协议:

下载


react-unite

Editable Grid & Layout

  1. yarn add react-unite

CAUTION: react-unite require react@16.7.0-alpha.0 & react-dom@16.7.0-alpha.0 to use react-hooks.

Sample project https://github.com/mizchi-sandbox/react-unite-example

Concept

  • inspired by Unity3D layout system
  • react-unite generates display: grid properties.

See also https://mizchi-sandbox.github.io/grid-generator/

EditableGrid (typescript)

  1. import React from "react";
  2. import ReactDOM from "react-dom";
  3. import { EditableGrid, GridArea } from "react-unite";
  4. const rows = ["1fr", "2fr", "1fr"];
  5. const columns = ["100px", "1fr", "1fr", "100px"];
  6. const areas = [
  7. ["a", "b", "c", "d"],
  8. ["e", "f", "g", "h"],
  9. ["j", "k", "l", "m"]
  10. ];
  11. const root = document.querySelector(".root");
  12. ReactDOM.render(
  13. <EditableGrid
  14. width={640}
  15. height={480}
  16. spacerSize={16}
  17. rows={rows}
  18. columns={columns}
  19. areas={areas}
  20. >
  21. <GridArea name="a">
  22. <div>a</div>
  23. </GridArea>
  24. <GridArea name="b">
  25. <div>b</div>
  26. </GridArea>
  27. <GridArea name="d">
  28. <div>d</div>
  29. </GridArea>
  30. <GridArea name="f">
  31. <div>f</div>
  32. </GridArea>
  33. <GridArea name="h">
  34. <div>h</div>
  35. </GridArea>
  36. <GridArea name="l">
  37. <div>l</div>
  38. </GridArea>
  39. </EditableGrid>,
  40. root
  41. );

EditableLayout (typescript)

  1. import React from "react";
  2. import ReactDOM from "react-dom";
  3. import { LayoutData, Windowed, EditableLayout } from "react-unite";
  4. const initialLayoutData: LayoutData = {
  5. grid: {
  6. columns: ["1fr", "1fr"],
  7. rows: ["40px", "1fr", "1fr"],
  8. areas: [
  9. ["header", "header"],
  10. ["preview", "inspector"],
  11. ["assets", "inspector"]
  12. ]
  13. },
  14. windows: {
  15. "#scene": { displayName: "Scene", id: "#scene" },
  16. "#project": { displayName: "Project", id: "#project" },
  17. "#hierachy": { displayName: "Hierachy", id: "#hierachy" },
  18. "#inspector": { displayName: "Inspector", id: "#inspector" },
  19. "#services": { displayName: "Services", id: "#services" }
  20. },
  21. panes: [
  22. {
  23. id: "preview",
  24. displayName: "Preview",
  25. selectedId: "#scene",
  26. windowIds: ["#scene"]
  27. },
  28. {
  29. id: "assets",
  30. displayName: "Preview",
  31. selectedId: "#project",
  32. windowIds: ["#project", "#hierachy"]
  33. },
  34. {
  35. id: "inspector",
  36. displayName: "Inspector",
  37. selectedId: "#inspector",
  38. windowIds: ["#inspector", "#services"]
  39. }
  40. ]
  41. };
  42. // To fill window, Set css `html, body { margin: 0;}`
  43. const MyLayout = () => {
  44. return (
  45. <Windowed>
  46. {(width, height) => (
  47. <EditableLayout
  48. width={width}
  49. height={height}
  50. layout={initialLayoutData}
  51. renderTab={data => {
  52. return <span>{data.displayName}</span>;
  53. }}
  54. renderWindow={win => {
  55. return (
  56. <div>
  57. {win.id}: {win.displayName}
  58. </div>
  59. );
  60. }}
  61. />
  62. )}
  63. </Windowed>
  64. );
  65. };
  66. const root = document.querySelector(".root");
  67. ReactDOM.render(<MyLayout ></MyLayout>, root);

How to dev

  • yarn build: Generate dist by rollup
  • yarn test: Run jest

Write your own grid renderer

  1. import styled from "styled-components";
  2. export const Grid: React.ComponentClass<GridProps> = styled.div`
  3. display: grid;
  4. width: ${(p: GridProps) => p.width || "100%"};
  5. height: ${(p: GridProps) => p.height || "100%"};
  6. grid-template-columns: ${(p: GridProps) => p.columns.join(" ")};
  7. grid-template-rows: ${(p: GridProps) => p.rows.join(" ")};
  8. grid-template-areas: ${(p: GridProps) =>
  9. p.areas.map(row => "'" + row.join(" ") + "'").join(" ")};
  10. `;

TODO

  • Publish
  • Swap window on drop
  • Remove div in src
  • renderTab
  • Visual Effect on dragStart and dragEnd
  • Fill Component
  • containerType: one | tabs | nest
  • ResizableBox
  • Option: resizableRows
  • Option: resizableColumns
  • Option: minimumWindowWidth
  • Option: minimumWindowHeight
  • Option: editable: boolean

LICENSE

MIT