Skip to content

Commit

Permalink
feat: add segment control
Browse files Browse the repository at this point in the history
  • Loading branch information
laurelfulford committed May 14, 2024
1 parent 0a4f253 commit 995fbc5
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 13 deletions.
1 change: 1 addition & 0 deletions assets/newspack-ui/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import './style.scss';
import './js/index.js';
1 change: 1 addition & 0 deletions assets/newspack-ui/js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './segmented-control';
34 changes: 34 additions & 0 deletions assets/newspack-ui/js/segmented-control/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { domReady } from '../utils'; // Global utils.

domReady( function () {
function segmented_control( element ) {
const header = element.querySelector( '.newspack-ui__segmented-control__tabs' );
const tab_headers = [ ...header.children ];
const tab_body = element.querySelector( '.newspack-ui__segmented-control__content' );
let tab_contents = [];

if ( null !== tab_body ) {
tab_contents = [ ...tab_body.children ];
}

tab_headers.forEach( ( tab, i ) => {
if ( tab_contents.length !== 0 && tab.classList.contains( 'selected' ) ) {
tab_contents[ i ].classList.add( 'selected' );
}

tab.addEventListener( 'click', function () {
tab_headers.forEach( t => t.classList.remove( 'selected' ) );
this.classList.add( 'selected' );

if ( tab_contents.length !== 0 ) {
tab_contents.forEach( content => content.classList.remove( 'selected' ) );
tab_contents[ i ].classList.add( 'selected' );
}
} );
} );
}

[ ...document.querySelectorAll( '.newspack-ui__segmented-control' ) ].forEach( x =>
segmented_control( x )
);
} );
25 changes: 25 additions & 0 deletions assets/newspack-ui/js/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Util functions.
*/

/**
* Specify a function to execute when the DOM is fully loaded.
*
* @see https://github.com/WordPress/gutenberg/blob/trunk/packages/dom-ready/
*
* @param {Function} callback A function to execute after the DOM is ready.
* @return {void}
*/
export const domReady = callback => {
if ( typeof document === 'undefined' ) {
return;
}
if (
document.readyState === 'complete' || // DOMContentLoaded + Images/Styles/etc loaded, so we call directly.
document.readyState === 'interactive' // DOMContentLoaded fires at this point, so we call directly.
) {
return void callback();
}
// DOMContentLoaded has not fired yet, delay callback until then.
document.addEventListener( 'DOMContentLoaded', callback );
};
1 change: 1 addition & 0 deletions assets/newspack-ui/scss/elements/_index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
@use 'forms';
@use 'icons';
@use 'misc';
@use 'segmented-control';
@use 'tables';
@use 'typography';
@use 'woocommerce';
43 changes: 43 additions & 0 deletions assets/newspack-ui/scss/elements/_segmented-control.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
.newspack-ui {
&__segmented-control {
align-items: center;
display: flex;
gap: var( --newspack-ui-spacer-2 );
flex-direction: column;

&:not( :last-child ) {
margin-bottom: var( --newspack-ui-spacer-2 );
}

&__tabs {
background: var( --newspack-ui-color-neutral-10 );
border-radius: var( --newspack-ui-border-radius-m );
display: flex;
flex-wrap: wrap;
gap: calc( var( --newspack-ui-spacer-base ) / 2 );
justify-content: center;
padding: calc( var( --newspack-ui-spacer-base ) / 2 );

.newspack-ui__button {
background: transparent;
border: 1px solid transparent;
color: var( --newspack-ui-color-neutral-60 );
margin: 0;

&.selected {
background: var( --newspack-ui-color-neutral-0 );
border-color: var( --newspack-ui-color-neutral-30 );
color: var( --newspack-ui-color-neutral-90 );
}
}
}

&__content {
width: 100%;

> *:not( .selected ) {
display: none;
}
}
}
}
34 changes: 29 additions & 5 deletions assets/newspack-ui/scss/elements/forms/_buttons.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
font-family: var( --newspack-ui-font-family );
font-size: var( --newspack-ui-font-size-s );
font-weight: 600;
gap: var( --newspack-ui-spacer-2 );
gap: calc( var( --newspack-ui-spacer-base ) / 2 );
justify-content: center;
line-height: var( --newspack-ui-line-height-s );
padding: var( --newspack-ui-spacer-2 ) var( --newspack-ui-spacer-4 );
min-height: var( --newspack-ui-spacer-9 );
padding: var( --newspack-ui-spacer-2 ) var( --newspack-ui-spacer-5 );
transition: background-color 125ms ease-in-out, border-color 125ms ease-in-out, outline 125ms ease-in-out;

&:not( :last-child ) {
Expand All @@ -30,6 +31,26 @@
outline-offset: 1px;
}

// Sizes
&--x-small {
border-radius: var( --newspack-ui-border-radius-s );
font-size: var( --newspack-ui-font-size-xs );
line-height: var( --newspack-ui-line-height-xs );
min-height: var( --newspack-ui-spacer-6 );
padding: calc( var( --newspack-ui-spacer-base ) / 2 ) var( --newspack-ui-spacer-base );
}

&--small {
font-size: var( --newspack-ui-font-size-xs );
line-height: var( --newspack-ui-line-height-xs );
min-height: var( --newspack-ui-spacer-8 );
padding: var( --newspack-ui-spacer-base ) var( --newspack-ui-spacer-3 );
}

&--medium {
// is the default.
}

// Styles
&--primary {
background: var( --newspack-ui-color-button-bg );
Expand Down Expand Up @@ -139,13 +160,16 @@
&--icon {
display: grid;
height: var( --newspack-ui-spacer-7 ); // TODO: This is the XS size, we will need S, and M
min-height: unset;
padding: 0;
place-items: center;
width: var( --newspack-ui-spacer-7 );

svg {
fill: currentcolor;
}

}

svg {
fill: currentcolor;
}
}
}
112 changes: 104 additions & 8 deletions includes/class-newspack-ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ public static function enqueue_styles() {
[],
NEWSPACK_PLUGIN_VERSION
);

wp_enqueue_script(
'newspack-ui',
Newspack::plugin_url() . '/dist/newspack-ui.js',
[],
NEWSPACK_PLUGIN_VERSION,
true
);
}

/**
Expand Down Expand Up @@ -269,31 +277,119 @@ public static function return_demo_content() {
<path fill-rule="evenodd" clip-rule="evenodd" d="M4.405 11.9C4.205 11.3 4.091 10.66 4.091 10C4.091 9.34001 4.205 8.70001 4.405 8.10001V5.51001H1.064C0.364015 6.90321 -0.000359433 8.44084 2.66054e-07 10C2.66054e-07 11.614 0.386 13.14 1.064 14.49L4.404 11.9H4.405Z" fill="#FBBC05"></path>
<path fill-rule="evenodd" clip-rule="evenodd" d="M9.99996 3.977C11.468 3.977 12.786 4.482 13.823 5.473L16.691 2.605C14.959 0.99 12.695 0 9.99996 0C6.08996 0 2.70996 2.24 1.06396 5.51L4.40396 8.1C5.19196 5.736 7.39596 3.977 9.99996 3.977Z" fill="#EA4335"></path>
</svg>
<span>
Sign in with Google
</span>
Sign in with Google
</button>
<button class="newspack-ui__button newspack-ui__button--wide">Default Theme Button</button>
<button class="newspack-ui__button newspack-ui__button--primary newspack-ui__button--wide">Primary Button</button>
<button class="newspack-ui__button newspack-ui__button--branded newspack-ui__button--wide">Branded Button</button>
<button class="newspack-ui__button newspack-ui__button--secondary newspack-ui__button--wide">Secondary Button</button>
<button class="newspack-ui__button newspack-ui__button--ghost newspack-ui__button--wide">Ghost Button</button>
<button class="newspack-ui__button newspack-ui__button--outline newspack-ui__button--wide">Outline Button</button>
<button class="newspack-ui__button newspack-ui__button--destructive newspack-ui__button--wide">Destructive Button</button>
<button class="newspack-ui__button newspack-ui__button--secondary newspack-ui__button--wide">
<svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M19.6 10.227C19.6 9.51801 19.536 8.83701 19.418 8.18201H10V12.05H15.382C15.2706 12.6619 15.0363 13.2448 14.6932 13.7635C14.3501 14.2822 13.9054 14.726 13.386 15.068V17.578H16.618C18.509 15.836 19.6 13.273 19.6 10.228V10.227Z" fill="#4285F4"></path>
<path fill-rule="evenodd" clip-rule="evenodd" d="M9.99996 20C12.7 20 14.964 19.105 16.618 17.577L13.386 15.068C12.491 15.668 11.346 16.023 9.99996 16.023C7.39496 16.023 5.18996 14.263 4.40496 11.9H1.06396V14.49C1.89597 16.1468 3.17234 17.5395 4.7504 18.5126C6.32846 19.4856 8.14603 20.0006 9.99996 20Z" fill="#34A853"></path>
<path fill-rule="evenodd" clip-rule="evenodd" d="M4.405 11.9C4.205 11.3 4.091 10.66 4.091 10C4.091 9.34001 4.205 8.70001 4.405 8.10001V5.51001H1.064C0.364015 6.90321 -0.000359433 8.44084 2.66054e-07 10C2.66054e-07 11.614 0.386 13.14 1.064 14.49L4.404 11.9H4.405Z" fill="#FBBC05"></path>
<path fill-rule="evenodd" clip-rule="evenodd" d="M9.99996 3.977C11.468 3.977 12.786 4.482 13.823 5.473L16.691 2.605C14.959 0.99 12.695 0 9.99996 0C6.08996 0 2.70996 2.24 1.06396 5.51L4.40396 8.1C5.19196 5.736 7.39596 3.977 9.99996 3.977Z" fill="#EA4335"></path>
</svg>
<span>
Sign up with Google
</span>
Sign up with Google
</button>

<hr>

<button class="newspack-ui__button newspack-ui__button--primary newspack-ui__button--x-small">X-Small Button</button><br />
<button class="newspack-ui__button newspack-ui__button--primary newspack-ui__button--x-small">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M7.25 16.4371C6.16445 15.2755 5.5 13.7153 5.5 12C5.5 8.41015 8.41015 5.5 12 5.5C15.5899 5.5 18.5 8.41015 18.5 12C18.5 13.7153 17.8356 15.2755 16.75 16.4371V16C16.75 14.4812 15.5188 13.25 14 13.25L10 13.25C8.48122 13.25 7.25 14.4812 7.25 16V16.4371ZM8.75 17.6304C9.70606 18.1835 10.8161 18.5 12 18.5C13.1839 18.5 14.2939 18.1835 15.25 17.6304V16C15.25 15.3096 14.6904 14.75 14 14.75L10 14.75C9.30964 14.75 8.75 15.3096 8.75 16V17.6304ZM4 12C4 7.58172 7.58172 4 12 4C16.4183 4 20 7.58172 20 12C20 16.4183 16.4183 20 12 20C7.58172 20 4 16.4183 4 12ZM14 10C14 11.1046 13.1046 12 12 12C10.8954 12 10 11.1046 10 10C10 8.89543 10.8954 8 12 8C13.1046 8 14 8.89543 14 10Z"/>
</svg>
X-Small Button
</button><br />
<button class="newspack-ui__button newspack-ui__button--primary newspack-ui__button--x-small">
X-Small Button
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M7.25 16.4371C6.16445 15.2755 5.5 13.7153 5.5 12C5.5 8.41015 8.41015 5.5 12 5.5C15.5899 5.5 18.5 8.41015 18.5 12C18.5 13.7153 17.8356 15.2755 16.75 16.4371V16C16.75 14.4812 15.5188 13.25 14 13.25L10 13.25C8.48122 13.25 7.25 14.4812 7.25 16V16.4371ZM8.75 17.6304C9.70606 18.1835 10.8161 18.5 12 18.5C13.1839 18.5 14.2939 18.1835 15.25 17.6304V16C15.25 15.3096 14.6904 14.75 14 14.75L10 14.75C9.30964 14.75 8.75 15.3096 8.75 16V17.6304ZM4 12C4 7.58172 7.58172 4 12 4C16.4183 4 20 7.58172 20 12C20 16.4183 16.4183 20 12 20C7.58172 20 4 16.4183 4 12ZM14 10C14 11.1046 13.1046 12 12 12C10.8954 12 10 11.1046 10 10C10 8.89543 10.8954 8 12 8C13.1046 8 14 8.89543 14 10Z"/>
</svg>
</button><br />
<button class="newspack-ui__button newspack-ui__button--primary newspack-ui__button--small">Small Button</button><br />
<button class="newspack-ui__button newspack-ui__button--primary newspack-ui__button--small">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M7.25 16.4371C6.16445 15.2755 5.5 13.7153 5.5 12C5.5 8.41015 8.41015 5.5 12 5.5C15.5899 5.5 18.5 8.41015 18.5 12C18.5 13.7153 17.8356 15.2755 16.75 16.4371V16C16.75 14.4812 15.5188 13.25 14 13.25L10 13.25C8.48122 13.25 7.25 14.4812 7.25 16V16.4371ZM8.75 17.6304C9.70606 18.1835 10.8161 18.5 12 18.5C13.1839 18.5 14.2939 18.1835 15.25 17.6304V16C15.25 15.3096 14.6904 14.75 14 14.75L10 14.75C9.30964 14.75 8.75 15.3096 8.75 16V17.6304ZM4 12C4 7.58172 7.58172 4 12 4C16.4183 4 20 7.58172 20 12C20 16.4183 16.4183 20 12 20C7.58172 20 4 16.4183 4 12ZM14 10C14 11.1046 13.1046 12 12 12C10.8954 12 10 11.1046 10 10C10 8.89543 10.8954 8 12 8C13.1046 8 14 8.89543 14 10Z"/>
</svg>
Small Button
</button><br />
<button class="newspack-ui__button newspack-ui__button--primary newspack-ui__button--small">
Small Button
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M7.25 16.4371C6.16445 15.2755 5.5 13.7153 5.5 12C5.5 8.41015 8.41015 5.5 12 5.5C15.5899 5.5 18.5 8.41015 18.5 12C18.5 13.7153 17.8356 15.2755 16.75 16.4371V16C16.75 14.4812 15.5188 13.25 14 13.25L10 13.25C8.48122 13.25 7.25 14.4812 7.25 16V16.4371ZM8.75 17.6304C9.70606 18.1835 10.8161 18.5 12 18.5C13.1839 18.5 14.2939 18.1835 15.25 17.6304V16C15.25 15.3096 14.6904 14.75 14 14.75L10 14.75C9.30964 14.75 8.75 15.3096 8.75 16V17.6304ZM4 12C4 7.58172 7.58172 4 12 4C16.4183 4 20 7.58172 20 12C20 16.4183 16.4183 20 12 20C7.58172 20 4 16.4183 4 12ZM14 10C14 11.1046 13.1046 12 12 12C10.8954 12 10 11.1046 10 10C10 8.89543 10.8954 8 12 8C13.1046 8 14 8.89543 14 10Z"/>
</svg>
</button><br />
<button class="newspack-ui__button newspack-ui__button--primary">
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M7.25 16.4371C6.16445 15.2755 5.5 13.7153 5.5 12C5.5 8.41015 8.41015 5.5 12 5.5C15.5899 5.5 18.5 8.41015 18.5 12C18.5 13.7153 17.8356 15.2755 16.75 16.4371V16C16.75 14.4812 15.5188 13.25 14 13.25L10 13.25C8.48122 13.25 7.25 14.4812 7.25 16V16.4371ZM8.75 17.6304C9.70606 18.1835 10.8161 18.5 12 18.5C13.1839 18.5 14.2939 18.1835 15.25 17.6304V16C15.25 15.3096 14.6904 14.75 14 14.75L10 14.75C9.30964 14.75 8.75 15.3096 8.75 16V17.6304ZM4 12C4 7.58172 7.58172 4 12 4C16.4183 4 20 7.58172 20 12C20 16.4183 16.4183 20 12 20C7.58172 20 4 16.4183 4 12ZM14 10C14 11.1046 13.1046 12 12 12C10.8954 12 10 11.1046 10 10C10 8.89543 10.8954 8 12 8C13.1046 8 14 8.89543 14 10Z"/>
</svg>
Medium Button (default)
</button><br />
<button class="newspack-ui__button newspack-ui__button--primary">
Medium Button (default)
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M7.25 16.4371C6.16445 15.2755 5.5 13.7153 5.5 12C5.5 8.41015 8.41015 5.5 12 5.5C15.5899 5.5 18.5 8.41015 18.5 12C18.5 13.7153 17.8356 15.2755 16.75 16.4371V16C16.75 14.4812 15.5188 13.25 14 13.25L10 13.25C8.48122 13.25 7.25 14.4812 7.25 16V16.4371ZM8.75 17.6304C9.70606 18.1835 10.8161 18.5 12 18.5C13.1839 18.5 14.2939 18.1835 15.25 17.6304V16C15.25 15.3096 14.6904 14.75 14 14.75L10 14.75C9.30964 14.75 8.75 15.3096 8.75 16V17.6304ZM4 12C4 7.58172 7.58172 4 12 4C16.4183 4 20 7.58172 20 12C20 16.4183 16.4183 20 12 20C7.58172 20 4 16.4183 4 12ZM14 10C14 11.1046 13.1046 12 12 12C10.8954 12 10 11.1046 10 10C10 8.89543 10.8954 8 12 8C13.1046 8 14 8.89543 14 10Z"/>
</svg>
</button>

<hr>

<div class="newspack-ui__segmented-control">
<div class="newspack-ui__segmented-control__tabs">
<button class="newspack-ui__button newspack-ui__button--small selected">Tab One</button>
<button class="newspack-ui__button newspack-ui__button--small">Tab Two</button>
<button class="newspack-ui__button newspack-ui__button--small">Tab Three</button>
<button class="newspack-ui__button newspack-ui__button--small">Tab Four</button>
<button class="newspack-ui__button newspack-ui__button--small">Tab Five</button>
</div>
</div>

<div class="newspack-ui__segmented-control">
<div class="newspack-ui__segmented-control__tabs">
<button class="newspack-ui__button newspack-ui__button--small">Annually</button>
<button class="newspack-ui__button newspack-ui__button--small selected">Monthly</button>
</div>
<div class="newspack-ui__segmented-control__content">
<div>
<label class="newspack-ui__input-list">
<input type="checkbox" name="checkbox-option-1">
<span>
<strong>Annual Option 1</strong>
<span class="newspack-ui__helper-text">Friday roundup of the most relevant stories.</span>
</span>
</label>
<label class="newspack-ui__input-list">
<input type="checkbox" name="checkbox-option-1">
<span>
<strong>Annual Option 2</strong>
<span class="newspack-ui__helper-text">Friday roundup of the most relevant stories.</span>
</span>
</label>
</div>
<div>
<label class="newspack-ui__input-list">
<input type="checkbox" name="checkbox-option-1">
<span>
<strong>Montly Option 1</strong>
<span class="newspack-ui__helper-text">Friday roundup of the most relevant stories.</span>
</span>
</label>
<label class="newspack-ui__input-list">
<input type="checkbox" name="checkbox-option-1">
<span>
<strong>Monthly Option 2</strong><br>
<span class="newspack-ui__helper-text">Friday roundup of the most relevant stories.</span>
</span>
</label>
</div>
</div>
</div>

<hr>

<h2>Buttons Icon</h2>
<p>Uses the same classes as the <code>newspack-ui__button</code> but we add an extra class to it <code>newspack-ui__button--icon</code></p>
<button class="newspack-ui__button newspack-ui__button--icon">
Expand Down

0 comments on commit 995fbc5

Please sign in to comment.