Skip to content

Commit 6feba96

Browse files
committed
fix: prevent layout properties to be set on children
1 parent 394c159 commit 6feba96

File tree

1 file changed

+45
-8
lines changed

1 file changed

+45
-8
lines changed

src/index.js

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,40 @@ const isWindows = 'navigator' in global && /Win/i.test(navigator.platform);
6565
const isMacLike =
6666
'navigator' in global && /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);
6767

68-
const className = 'npm__react-simple-code-editor__textarea';
68+
const container = 'npm__react-simple-code-editor__container';
6969

7070
const cssText = /* CSS */ `
71+
/**
72+
* Make sure that layout related properties cannot be set on the children
73+
* They will likely break the editor when set. The user can always use !important to force override.
74+
*/
75+
.${container} > pre * {
76+
display: inline;
77+
font-family: inherit;
78+
font-size: inherit;
79+
font-style: inherit;
80+
font-variant-ligatures: inherit;
81+
font-weight: inherit;
82+
letter-spacing: inherit;
83+
line-height: inherit;
84+
margin-top: 0;
85+
margin-right: 0;
86+
margin-bottom: 0;
87+
margin-left: 0;
88+
padding-top: 0;
89+
padding-right: 0;
90+
padding-bottom: 0;
91+
padding-left: 0;
92+
border-top: 0;
93+
border-right: 0;
94+
border-bottom: 0;
95+
border-left: 0;
96+
}
97+
7198
/**
7299
* Reset the text fill color so that placeholder is visible
73100
*/
74-
.${className}:empty {
101+
.${container} > textarea:empty {
75102
-webkit-text-fill-color: inherit !important;
76103
}
77104
@@ -84,11 +111,11 @@ const cssText = /* CSS */ `
84111
* So we use 'color: transparent' to make the text transparent on IE
85112
* Unlike other browsers, it doesn't affect caret color in IE
86113
*/
87-
.${className} {
114+
.${container} > textarea {
88115
color: transparent !important;
89116
}
90117
91-
.${className}::selection {
118+
.${container} > textarea::selection {
92119
background-color: #accef7 !important;
93120
color: transparent !important;
94121
}
@@ -498,6 +525,7 @@ export default class Editor extends React.Component<Props, State> {
498525
const {
499526
value,
500527
style,
528+
className,
501529
padding,
502530
highlight,
503531
textareaId,
@@ -534,15 +562,18 @@ export default class Editor extends React.Component<Props, State> {
534562
const highlighted = highlight(value);
535563

536564
return (
537-
<div {...rest} style={{ ...styles.container, ...style }}>
565+
<div
566+
{...rest}
567+
style={{ ...styles.container, ...style }}
568+
className={`${container} ${className || ''}`}
569+
>
538570
<textarea
539571
ref={c => (this._input = c)}
540572
style={{
541573
...styles.editor,
542574
...styles.textarea,
543575
...contentStyle,
544576
}}
545-
className={className}
546577
id={textareaId}
547578
value={value}
548579
onChange={this._handleChange}
@@ -606,8 +637,14 @@ const styles = {
606637
pointerEvents: 'none',
607638
},
608639
editor: {
609-
margin: 0,
610-
border: 0,
640+
marginTop: 0,
641+
marginRight: 0,
642+
marginBottom: 0,
643+
marginLeft: 0,
644+
borderTop: 0,
645+
borderRight: 0,
646+
borderBottom: 0,
647+
borderLeft: 0,
611648
background: 'none',
612649
boxSizing: 'inherit',
613650
display: 'inherit',

0 commit comments

Comments
 (0)