Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <ground-control control-for=".major-tom" set-prop="--scale">
	<label for="font-scale">Font scale</label>
	<input id="font-scale" type="range" min="0" max="3" step="0.1" value="1" />
</ground-control>

<ground-control control-for=".major-tom" set-attr="color-mode">
	<label for="color-mode">Color Mode</label>
	<select id="color-mode">
		<option value="light">light</option>
		<option value="dark">dark</option>
		<option value="rainbow">rainbow</option>
	</select>
	<button reset-for="color-mode">reset</button>
</ground-control>

<ground-control control-for="div" set-prop="border">
	<label for="border">Border</label>
	<input id="border" type="text" value="medium solid teal">
</ground-control>	

<button reset-all>Reset all</button>

<div class="major-tom">
	<h1>&lt;Ground-Control&gt;</h1>
	<ul>
		<li>
			Scale: <output for="font-scale" with-units="vw">N/A</output>
		</li>
		<li>
			Mode: <output for="color-mode">N/A</output>
		</li>
	</ul>
	<p>
		This web component is based on
		Eric Meyer's
		<a href="https://codepen.io/meyerweb/pen/PoVPbzK"><code>super-slider</code> component</a>
		and excellent article
		<a href="https://meyerweb.com/eric/thoughts/2023/11/01/blinded-by-the-light-dom/">
			<em>Blinded By the Light DOM</em></a>.
		My goal was to make it a bit more generic,
		for any <code>input</code> to control
		the properties or attributes
		on any number of target elements.
		It also finds any related <code>ouptut</code> elements,
		and keeps them up to date with changes.
	</p>
	<p>
		This experiment keeps escalating,
		as Eric and I continue to fork our forks of forks,
		<a href="https://codepen.io/meyerweb/pen/BaMZmVg">borrowing and sharing ideas</a>,
		and learning as we go.
		<em>I am not a web component expert</em>,
		and may be doing it all wrong.
	</p>
</div>

              
            
!

CSS

              
                /* 
	The styles are just to visualize results,
	they are not part of the component.
*/

* { box-sizing: border-box; }
html { font-family: sans-serif; }

ground-control {
	display: block;
	margin-block: 1em;
}

label {
	display: block;
	font-weight: bold;
}

.major-tom {
	font-size: calc(1em + (1vw * var(--scale)));
	margin-block: 1em;
}

output {
	font-family: monospace;
	background: linear-gradient(
		transparent 0 50%, 
		var(--output-highlight, papayawhip) 50% 100%
	);
	padding-inline: 0.5em;
}

[color-mode] {
	background: var(--bg, white);
	border: thin solid;
	color: var(--text, black);
	padding: 1em;
	
	& a:any-link {
		color: var(--link, blue);
	}
}

[color-mode='dark'] {
	--bg: black;
	--text: white;
	--link: cyan;
	--output-highlight: mediumvioletred;
}

[color-mode='rainbow'] {
	--bg: 
	linear-gradient(hsl(0deg 0% 100% / 0.9), hsl(0deg 0% 100% / 0.9)),
	linear-gradient(to bottom right,
		maroon 0 12%,
		orange 12% 24%,
		yellow 24% 36%,
		teal 36% 48%,
		midnightblue 48% 60%,
		indigo 60% 72%,
		darkmagenta 72% 84%,
		mediumvioletred 84%
	);
	--output-highlight: cyan;
	--link: mediumvioletred;
	text-shadow: 0 1px 0 white;
	background-size: calc(100% - 1em) calc(100% - 1em), 100% 100%;
	background-repeat: no-repeat;
	background-position: center;
}
              
            
!

JS

              
                class groundControl extends HTMLElement {
  static observedAttributes = [
    'control-for',
    'set-attr',
    'set-prop',
  ];

  static attrToPropMap = {
    'control-for': 'targetSelect',
    'set-attr': 'setAttr',
    'set-prop': 'setProp',
  };

  static controls = [
    'input',
    'select'
  ];

  inputEl;
  resetValue;
  targetEls;
  displayEls;
  setProp;
  setAttr;

	targetSelect = ':root';

  constructor() {
    super();

    // Add direct event listeners when component is created
    this.addEventListener('reset', (e) => this.resetControl());
  }

  // attribute changes
  attributeChangedCallback(name, oldValue, newValue) {
    this[groundControl.attrToPropMap[name]] = newValue;
    if (this.inputEl) { this.setUp(); }
  }

  // first connection
  connectedCallback() {
    this.inputEl = this.querySelector(groundControl.controls.join(', '));

    if (this.inputEl) {
      this.setUp();
      this.broadCast();

      this.inputEl.addEventListener('input', (e) => this.broadCast());
      this.resetBtn?.addEventListener('click', (e) => this.resetControl());
    } else {
      console.error("Your circuit's dead, there's something wrong (no input/select found)");
    }
  }

  disconnectedCallback() {
    this.inputEl.removeEventListener('input', (e) => this.broadCast());
    this.resetBtn?.removeEventListener('click', (e) => this.resetControl());
  }

  // methods
  setUp = () => {
    this.resetValue = this.inputEl.value;

    const selectors = {
      target: this.targetSelect || ':root',
      output: `output[for=${this.inputEl.id}]`,
      reset: `button[reset-for=${this.inputEl.id}]`,
    };

    this.targetEls = document.querySelectorAll(selectors.target);
    this.displayEls = document.querySelectorAll(selectors.output);
    this.resetBtn = this.querySelector(selectors.reset);
  };

  // change target attrs/props, and update output displayEls
  broadCast = () => {
    this.targetEls.forEach((el) => {
      if (this.setProp) {
        el.style.setProperty(this.setProp, this.inputEl.value);
      }
      if (this.setAttr) {
        el.setAttribute(this.setAttr, this.inputEl.value);
      }
    });

    this.displayEls.forEach((el) => {
      const units = el.getAttribute('with-units') || '';
      el.value = `${this.inputEl.value}${units}`;
    });
  };

  // reset the value of the control
  resetControl = (value) => {
    this.inputEl.value = value || this.resetValue;
    this.broadCast();
  };
}

// register the ground-control element
customElements.define("ground-control", groundControl);

// the reset-all button is not part of the component,
// but triggers the ground-control 'reset' event
document
	.querySelectorAll('[reset-all]:is(button, [type=button])')
	.forEach((btn) => {
		btn.addEventListener('click', (e) => {
			document.querySelectorAll('ground-control').forEach((control) => {
				control.dispatchEvent(
					new Event("reset", { view: window, bubbles: false })
				);
			});
		});
});
              
            
!
999px

Console