feat(styles): one stylesheet source for react and svelte (#3350)

* feat(styles): create stylesheets for react and svelte based on one source

* refactor(styling): cleanup

* refactor(postcss): share a config

* refactor(postcss): replace env hack with env file
This commit is contained in:
Moritz Klack
2023-08-24 14:02:57 +02:00
committed by GitHub
parent d2c23c5149
commit bd922889b4
47 changed files with 1422 additions and 962 deletions
@@ -1,4 +1,4 @@
import { memo, useRef } from 'react';
import { CSSProperties, memo, useRef } from 'react';
import cc from 'classcat';
import { shallow } from 'zustand/shallow';
@@ -6,12 +6,7 @@ import { useStore } from '../../hooks/useStore';
import { type ReactFlowState } from '../../types';
import { BackgroundProps, BackgroundVariant } from './types';
import { DotPattern, LinePattern } from './Patterns';
const defaultColor = {
[BackgroundVariant.Dots]: '#91919a',
[BackgroundVariant.Lines]: '#eee',
[BackgroundVariant.Cross]: '#e2e2e2',
};
import { containerStyle } from '../../styles/utils';
const defaultSize = {
[BackgroundVariant.Dots]: 1,
@@ -31,12 +26,13 @@ function Background({
lineWidth = 1,
offset = 2,
color,
bgColor,
style,
className,
patternClassName,
}: BackgroundProps) {
const ref = useRef<SVGSVGElement>(null);
const { transform, patternId } = useStore(selector, shallow);
const patternColor = color || defaultColor[variant];
const patternSize = size || defaultSize[variant];
const isDots = variant === BackgroundVariant.Dots;
const isCross = variant === BackgroundVariant.Cross;
@@ -53,14 +49,14 @@ function Background({
return (
<svg
className={cc(['react-flow__background', className])}
style={{
...style,
position: 'absolute',
width: '100%',
height: '100%',
top: 0,
left: 0,
}}
style={
{
...style,
...containerStyle,
'--background-color-props': bgColor,
'--background-pattern-color-props': color,
} as CSSProperties
}
ref={ref}
data-testid="rf__background"
>
@@ -74,9 +70,14 @@ function Background({
patternTransform={`translate(-${patternOffset[0]},-${patternOffset[1]})`}
>
{isDots ? (
<DotPattern color={patternColor} radius={scaledSize / offset} />
<DotPattern radius={scaledSize / offset} className={patternClassName} />
) : (
<LinePattern dimensions={patternDimensions} color={patternColor} lineWidth={lineWidth} />
<LinePattern
dimensions={patternDimensions}
lineWidth={lineWidth}
variant={variant}
className={patternClassName}
/>
)}
</pattern>
<rect x="0" y="0" width="100%" height="100%" fill={`url(#${patternId + id})`} />
@@ -1,30 +1,31 @@
import cc from 'classcat';
import { BackgroundVariant } from './types';
type LinePatternProps = {
dimensions: [number, number];
variant: BackgroundVariant;
lineWidth?: number;
color: string;
className?: string;
};
export function LinePattern({
color,
dimensions,
lineWidth,
}: LinePatternProps) {
export function LinePattern({ dimensions, lineWidth, variant, className }: LinePatternProps) {
return (
<path
stroke={color}
strokeWidth={lineWidth}
d={`M${dimensions[0] / 2} 0 V${dimensions[1]} M0 ${dimensions[1] / 2} H${
dimensions[0]
}`}
d={`M${dimensions[0] / 2} 0 V${dimensions[1]} M0 ${dimensions[1] / 2} H${dimensions[0]}`}
className={cc(['react-flow__background-pattern', variant, className])}
/>
);
}
type DotPatternProps = {
radius: number;
color: string;
className?: string;
};
export function DotPattern({ color, radius }: DotPatternProps) {
return <circle cx={radius} cy={radius} r={radius} fill={color} />;
export function DotPattern({ radius, className }: DotPatternProps) {
return (
<circle cx={radius} cy={radius} r={radius} className={cc(['react-flow__background-pattern', 'dot', className])} />
);
}
@@ -7,9 +7,11 @@ export enum BackgroundVariant {
}
export type BackgroundProps = {
id?: string
id?: string;
color?: string;
bgColor?: string;
className?: string;
patternClassName?: string;
gap?: number | [number, number];
size?: number;
offset?: number;
@@ -4,7 +4,7 @@ import { internalsSymbol, errorMessages, Position, clampPosition, getPositionWit
import useVisibleNodes from '../../hooks/useVisibleNodes';
import { useStore } from '../../hooks/useStore';
import { containerStyle } from '../../styles';
import { containerStyle } from '../../styles/utils';
import { GraphViewProps } from '../GraphView';
import type { NodeTypesWrapped, ReactFlowState, WrapNodeProps } from '../../types';
+1 -1
View File
@@ -8,7 +8,7 @@ import cc from 'classcat';
import { getNodesInside, getEventPosition, SelectionMode } from '@xyflow/system';
import UserSelection from '../../components/UserSelection';
import { containerStyle } from '../../styles';
import { containerStyle } from '../../styles/utils';
import { useStore, useStoreApi } from '../../hooks/useStore';
import { getSelectionChanges, getConnectedEdges } from '../../utils';
import type { ReactFlowProps, ReactFlowState, NodeChange, EdgeChange } from '../../types';
@@ -6,7 +6,7 @@ import { XYPanZoom, PanOnScrollMode, type Transform, type PanZoomInstance } from
import useKeyPress from '../../hooks/useKeyPress';
import useResizeHandler from '../../hooks/useResizeHandler';
import { useStore, useStoreApi } from '../../hooks/useStore';
import { containerStyle } from '../../styles';
import { containerStyle } from '../../styles/utils';
import type { FlowRendererProps } from '../FlowRenderer';
import type { ReactFlowState } from '../../types';
+2 -27
View File
@@ -1,28 +1,3 @@
/* this will be exported as base.css and can be used for a basic styling */
@import './init.css';
.react-flow__handle {
background-color: #333;
}
.react-flow__node-default,
.react-flow__node-input,
.react-flow__node-output,
.react-flow__node-group {
border-width: 1px;
border-style: solid;
border-color: #bbb;
&.selected,
&:focus,
&:focus-visible {
outline: none;
border: 1px solid #555;
}
}
.react-flow__nodesselection-rect,
.react-flow__selection {
background: rgba(150, 150, 180, 0.1);
border: 1px dotted rgba(155, 155, 155, 0.8);
}
@import '../../../system/src/styles/init.css';
@import '../../../system/src/styles/base.css';
-238
View File
@@ -1,238 +0,0 @@
/* these are the necessary styles for React Flow, they get used by base.css and style.css */
.react-flow__container {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
}
.react-flow__pane {
z-index: 1;
cursor: grab;
&.selection {
cursor: pointer;
}
&.dragging {
cursor: grabbing;
}
}
.react-flow__viewport {
transform-origin: 0 0;
z-index: 2;
pointer-events: none;
}
.react-flow__renderer {
z-index: 4;
}
.react-flow__selection {
z-index: 6;
}
.react-flow__nodesselection-rect:focus,
.react-flow__nodesselection-rect:focus-visible {
outline: none;
}
.react-flow .react-flow__edges {
pointer-events: none;
overflow: visible;
}
.react-flow__edge-path,
.react-flow__connection-path {
stroke: #b1b1b7;
stroke-width: 1;
fill: none;
}
.react-flow__edge {
pointer-events: visibleStroke;
cursor: pointer;
&.animated path {
stroke-dasharray: 5;
animation: dashdraw 0.5s linear infinite;
}
&.animated path.react-flow__edge-interaction {
stroke-dasharray: none;
animation: none;
}
&.inactive {
pointer-events: none;
}
&.selected,
&:focus,
&:focus-visible {
outline: none;
}
&.selected .react-flow__edge-path,
&:focus .react-flow__edge-path,
&:focus-visible .react-flow__edge-path {
stroke: #555;
}
&-textwrapper {
pointer-events: all;
}
&-textbg {
fill: white;
}
.react-flow__edge-text {
pointer-events: none;
user-select: none;
}
}
.react-flow__connection {
pointer-events: none;
.animated {
stroke-dasharray: 5;
animation: dashdraw 0.5s linear infinite;
}
}
.react-flow__connectionline {
z-index: 1001;
}
.react-flow__nodes {
pointer-events: none;
transform-origin: 0 0;
}
.react-flow__node {
position: absolute;
user-select: none;
pointer-events: all;
transform-origin: 0 0;
box-sizing: border-box;
cursor: grab;
&.dragging {
cursor: grabbing;
}
}
.react-flow__nodesselection {
z-index: 3;
transform-origin: left top;
pointer-events: none;
&-rect {
position: absolute;
pointer-events: all;
cursor: grab;
}
}
.react-flow__handle {
position: absolute;
pointer-events: none;
min-width: 5px;
min-height: 5px;
&.connectionindicator {
pointer-events: all;
cursor: crosshair;
}
&-bottom {
top: auto;
left: 50%;
bottom: -4px;
transform: translate(-50%, 0);
}
&-top {
left: 50%;
top: -4px;
transform: translate(-50%, 0);
}
&-left {
top: 50%;
left: -4px;
transform: translate(0, -50%);
}
&-right {
right: -4px;
top: 50%;
transform: translate(0, -50%);
}
}
.react-flow__edgeupdater {
cursor: move;
pointer-events: all;
}
.react-flow__panel {
position: absolute;
z-index: 5;
margin: 15px;
&.top {
top: 0;
}
&.bottom {
bottom: 0;
}
&.left {
left: 0;
}
&.right {
right: 0;
}
&.center {
left: 50%;
transform: translateX(-50%);
}
}
.react-flow__attribution {
font-size: 10px;
background: rgba(255, 255, 255, 0.5);
padding: 2px 3px;
margin: 0;
a {
text-decoration: none;
color: #999;
}
}
@keyframes dashdraw {
from {
stroke-dashoffset: 10;
}
}
.react-flow__edgelabel-renderer {
position: absolute;
width: 100%;
height: 100%;
pointer-events: none;
user-select: none;
}
.react-flow__minimap {
background-color: #fff;
}
-105
View File
@@ -1,105 +0,0 @@
.react-flow__resize-control {
position: absolute;
}
.react-flow__resize-control.left,
.react-flow__resize-control.right {
cursor: ew-resize;
}
.react-flow__resize-control.top,
.react-flow__resize-control.bottom {
cursor: ns-resize;
}
.react-flow__resize-control.top.left,
.react-flow__resize-control.bottom.right {
cursor: nwse-resize;
}
.react-flow__resize-control.bottom.left,
.react-flow__resize-control.top.right {
cursor: nesw-resize;
}
/* handle styles */
.react-flow__resize-control.handle {
width: 4px;
height: 4px;
border: 1px solid #fff;
border-radius: 1px;
background-color: #3367d9;
transform: translate(-50%, -50%);
}
.react-flow__resize-control.handle.left {
left: 0;
top: 50%;
}
.react-flow__resize-control.handle.right {
left: 100%;
top: 50%;
}
.react-flow__resize-control.handle.top {
left: 50%;
top: 0;
}
.react-flow__resize-control.handle.bottom {
left: 50%;
top: 100%;
}
.react-flow__resize-control.handle.top.left {
left: 0;
}
.react-flow__resize-control.handle.bottom.left {
left: 0;
}
.react-flow__resize-control.handle.top.right {
left: 100%;
}
.react-flow__resize-control.handle.bottom.right {
left: 100%;
}
/* line styles */
.react-flow__resize-control.line {
border-color: #3367d9;
border-width: 0;
border-style: solid;
}
.react-flow__resize-control.line.left,
.react-flow__resize-control.line.right {
width: 1px;
transform: translate(-50%, 0);
top: 0;
height: 100%;
}
.react-flow__resize-control.line.left {
left: 0;
border-left-width: 1px;
}
.react-flow__resize-control.line.right {
left: 100%;
border-right-width: 1px;
}
.react-flow__resize-control.line.top,
.react-flow__resize-control.line.bottom {
height: 1px;
transform: translate(0, -50%);
left: 0;
width: 100%;
}
.react-flow__resize-control.line.top {
top: 0;
border-top-width: 1px;
}
.react-flow__resize-control.line.bottom {
border-bottom-width: 1px;
top: 100%;
}
+3 -109
View File
@@ -1,110 +1,4 @@
/* this gets exported as style.css and can be used for the default theming */
@import './init.css';
@import './node-resizer.css';
.react-flow__edge {
&.updating {
.react-flow__edge-path {
stroke: #777;
}
}
&-text {
font-size: 10px;
}
}
.react-flow__node.selectable {
&:focus,
&:focus-visible {
outline: none;
}
}
.react-flow__node-default,
.react-flow__node-input,
.react-flow__node-output,
.react-flow__node-group {
padding: 10px;
border-radius: 3px;
width: 150px;
font-size: 12px;
color: #222;
text-align: center;
border-width: 1px;
border-style: solid;
border-color: #1a192b;
background-color: white;
&.selectable {
&:hover {
box-shadow: 0 1px 4px 1px rgba(0, 0, 0, 0.08);
}
&.selected,
&:focus,
&:focus-visible {
box-shadow: 0 0 0 0.5px #1a192b;
}
}
}
.react-flow__node-group {
background-color: rgba(240, 240, 240, 0.25);
}
.react-flow__nodesselection-rect,
.react-flow__selection {
background: rgba(0, 89, 220, 0.08);
border: 1px dotted rgba(0, 89, 220, 0.8);
&:focus,
&:focus-visible {
outline: none;
}
}
.react-flow__handle {
width: 6px;
height: 6px;
background: #1a192b;
border: 1px solid white;
border-radius: 100%;
}
.react-flow__controls {
box-shadow: 0 0 2px 1px rgba(0, 0, 0, 0.08);
&-button {
border: none;
background: #fefefe;
border-bottom: 1px solid #eee;
box-sizing: content-box;
display: flex;
justify-content: center;
align-items: center;
width: 16px;
height: 16px;
cursor: pointer;
user-select: none;
padding: 5px;
&:hover {
background: #f4f4f4;
}
svg {
width: 100%;
max-width: 12px;
max-height: 12px;
}
&:disabled {
pointer-events: none;
svg {
fill-opacity: 0.4;
}
}
}
}
@import '../../../system/src/styles/init.css';
@import '../../../system/src/styles/style.css';
@import '../../../system/src/styles/node-resizer.css';