Skip to content

Commit e9998c8

Browse files
committed
refactor(css): removes css, inline required CSS into <style>
Importing CSS files globally (e.g. `import "./styles.css"`) can cause resolution issues with certain libraries/frameworks. Example: Next.js (https://github.com/vercel/next.js/blob/master/errors/css-npm.md) Since rd3t's CSS is bare bones to begin with, we provide all required styles as a template string, which can be imported like any other TS/JS module and inlined into a `<style></style>` tag.
1 parent 527e71d commit e9998c8

File tree

9 files changed

+68
-66
lines changed

9 files changed

+68
-66
lines changed

package.json

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@
88
"url": "https://github.com/bkrem/react-d3-tree/issues"
99
},
1010
"homepage": "https://github.com/bkrem/react-d3-tree",
11-
"files": [
12-
"lib"
13-
],
11+
"files": ["lib"],
1412
"main": "lib/index.js",
1513
"scripts": {
16-
"cp:css": "cd src && find . -name '*.css' | cpio -pdm ../lib && cd ..",
1714
"build:docs": "rimraf ./docs && typedoc",
18-
"build": "rimraf lib && npm run cp:css && tsc",
19-
"build:watch": "rimraf lib && npm run cp:css && tsc -w",
15+
"build": "rimraf lib && tsc",
16+
"build:watch": "rimraf lib && tsc -w",
2017
"lint": "eslint src/**/*.js",
2118
"test": "jest --coverage --verbose",
2219
"test:clean": "rimraf ./coverage",
@@ -49,17 +46,8 @@
4946
}
5047
},
5148
"lint-staged": {
52-
"src/**/*.js": [
53-
"eslint",
54-
"prettier --write",
55-
"jest --findRelatedTests",
56-
"git add"
57-
],
58-
"src/**/*.{ts,tsx}": [
59-
"prettier --write",
60-
"jest --findRelatedTests",
61-
"git add"
62-
]
49+
"src/**/*.js": ["eslint", "prettier --write", "jest --findRelatedTests", "git add"],
50+
"src/**/*.{ts,tsx}": ["prettier --write", "jest --findRelatedTests", "git add"]
6351
},
6452
"dependencies": {
6553
"clone": "^2.1.1",

src/Link/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
TreeNodeDatum,
1111
PathClassFunction,
1212
} from '../types/common';
13-
import './style.css';
1413

1514
type LinkEventHandler = (
1615
source: HierarchyPointNode<TreeNodeDatum>,

src/Link/style.css

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/Node/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { select } from 'd3-selection';
44

55
import { Orientation, Point, TreeNodeDatum, RenderCustomNodeElementFn } from '../types/common';
66
import DefaultNodeElement from './DefaultNodeElement';
7-
import './style.css';
87

98
type NodeEventHandler = (id: string, evt: SyntheticEvent) => void;
109

src/Node/style.css

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/Tree/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Node from '../Node';
1111
import Link from '../Link';
1212
import { TreeNodeDatum, Point, RawNodeDatum } from '../types/common';
1313
import { TreeLinkEventCallback, TreeProps } from './types';
14-
import './style.css';
14+
import globalCss from '../globalCss';
1515

1616
type TreeState = {
1717
dataRef: TreeProps['data'];
@@ -489,6 +489,7 @@ class Tree extends React.Component<TreeProps, TreeState> {
489489

490490
return (
491491
<div className={`rd3t-tree-container ${zoomable ? 'rd3t-grabbable' : undefined}`}>
492+
<style>{globalCss}</style>
492493
<svg className={[rd3tSvgClassName, svgClassName].join(' ')} width="100%" height="100%">
493494
<TransitionGroupWrapper
494495
enableLegacyTransitions={enableLegacyTransitions}

src/Tree/style.css

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/globalCss.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Importing CSS files globally (e.g. `import "./styles.css"`) can cause resolution issues with certain
2+
// libraries/frameworks.
3+
// Example: Next.js (https://github.com/vercel/next.js/blob/master/errors/css-npm.md)
4+
//
5+
// Since rd3t's CSS is bare bones to begin with, we provide all required styles as a template string,
6+
// which can be imported like any other TS/JS module and inlined into a `<style></style>` tag.
7+
8+
export default `
9+
/* Tree */
10+
.rd3t-tree-container {
11+
width: 100%;
12+
height: 100%;
13+
}
14+
15+
.rd3t-grabbable {
16+
cursor: move; /* fallback if grab cursor is unsupported */
17+
cursor: grab;
18+
cursor: -moz-grab;
19+
cursor: -webkit-grab;
20+
}
21+
.rd3t-grabbable:active {
22+
cursor: grabbing;
23+
cursor: -moz-grabbing;
24+
cursor: -webkit-grabbing;
25+
}
26+
27+
/* Node */
28+
.rd3t-node {
29+
cursor: pointer;
30+
fill: #777;
31+
stroke: #000;
32+
stroke-width: 2;
33+
}
34+
35+
.rd3t-leaf-node {
36+
cursor: pointer;
37+
fill: transparent;
38+
stroke: #000;
39+
stroke-width: 2;
40+
}
41+
42+
.rd3t-label__title {
43+
stroke: #000;
44+
stroke-width: 1;
45+
}
46+
47+
.rd3t-label__attributes {
48+
stroke: #777;
49+
stroke-width: 1;
50+
font-size: smaller;
51+
}
52+
53+
/* Link */
54+
.rd3t-link {
55+
fill: none;
56+
stroke: #000;
57+
}
58+
`;

src/types/common.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { SyntheticEvent } from 'react';
12
import { HierarchyPointNode } from 'd3-hierarchy';
23

34
export type Orientation = 'horizontal' | 'vertical';
@@ -31,7 +32,7 @@ export type PathFunctionOption = 'diagonal' | 'elbow' | 'straight' | 'step';
3132
export type PathFunction = (link: TreeLinkDatum, orientation: Orientation) => string;
3233
export type PathClassFunction = PathFunction;
3334

34-
export type SyntheticEventHandler = (evt: React.SyntheticEvent) => void;
35+
export type SyntheticEventHandler = (evt: SyntheticEvent) => void;
3536

3637
/**
3738
* The properties that are passed to the user-defined `renderCustomNodeElement` render function.
@@ -50,4 +51,4 @@ export interface CustomNodeElementProps {
5051
toggleNode: () => void;
5152
}
5253

53-
export type RenderCustomNodeElementFn = (rd3tProps: CustomNodeElementProps) => JSX.Element;
54+
export type RenderCustomNodeElementFn = (rd3tNodeProps: CustomNodeElementProps) => JSX.Element;

0 commit comments

Comments
 (0)