Changeset 3826085 for tokenforge/trunk/includes/class-motion.php
- Timestamp:
- 11/26/2026 09:03:17 AM (less than one hour ago)
- File:
-
- 1 edited
-
tokenforge/trunk/includes/class-motion.php (modified) (16 diffs)
Legend:
- Unmodified
- Added
- Removed
-
tokenforge/trunk/includes/class-motion.php
r3823805 r3826085 13 13 * - Hover effects (.on-hover--*) and hover border effects (ripple, outline, 14 14 * underline). 15 * - Default button effects: one hover, entrance and border effect for every 16 * button, by adding the buttons to that effect's own rules. 15 17 * 16 18 * Effects compose (".on-enter--fade on-enter--float"), respect … … 140 142 'underline_width' => '3px', 141 143 'underline_duration' => '.3s', 144 // ---- Default button effects: one effect name from the lists above, '' = none ---- 145 'btn_hover_fx' => '', 146 'btn_enter_fx' => '', 147 'btn_border_fx' => '', 142 148 // ---- Builders ---- 143 149 'disable_in_builder' => true, // no scroll/visible effects in the Bricks canvas or block editor. … … 344 350 } 345 351 return implode( ',', array_merge( $single, $all ) ); 352 } 353 354 /** 355 * The buttons the default button effects apply to: the set Components::build() styles, at zero 356 * specificity, so an effect class on the element (or the site's own CSS) still wins. 357 * 358 * @return string 359 */ 360 private static function btn_sel() { 361 $c = Components::settings(); 362 $sel = '.btn,[class*="btn--"]:not(.btn--none,.wp-block-button),[class*="btn--"].wp-block-button>.wp-block-button__link'; 363 if ( ! empty( $c['control_bricks_buttons'] ) ) { 364 $sel .= ',.brxe-button.bricks-button,.wp-block-button__link,button:not([class]),input[type="submit"]'; 365 } 366 return ':where(' . $sel . ')'; 367 } 368 369 /** 370 * The default button effect a setting names, or '' (none or not one of the effects). 371 * 372 * @param array $s Settings. 373 * @param string $key btn_hover_fx|btn_enter_fx|btn_border_fx. 374 * @param array $fx The effects it may name. 375 * @return string 376 */ 377 private static function btn_fx( $s, $key, $fx ) { 378 return isset( $s[ $key ] ) && in_array( $s[ $key ], $fx, true ) ? $s[ $key ] : ''; 346 379 } 347 380 … … 493 526 $x_range = 'var(--exit-range-start, cover 50%) var(--exit-range-end, cover 100%)'; 494 527 if ( $s['enter'] ) { 528 // Default button entrance: buttons join the scroll rules only (no timed play on load, no 529 // fallback), so a button never waits on the script and simply shows where scroll timelines are missing. 530 $b_fx = self::btn_fx( $s, 'btn_enter_fx', self::SCROLL_FX ); 531 $b_sel = $b_fx ? ',' . self::btn_sel() : ''; 495 532 $scroll[] = '@keyframes tf-enter{from{' . $frame( '--tf-e' ) . '}}'; 496 $scroll[] = $enter . '{' . $ident( '--tf-e' ) . ';animation-name:tf-enter;animation-duration:auto;animation-timing-function:linear;animation-fill-mode:both;animation-timeline:view();animation-range:' . $e_range . '}';533 $scroll[] = $enter . $b_sel . '{' . $ident( '--tf-e' ) . ';animation-name:tf-enter;animation-duration:auto;animation-timing-function:linear;animation-fill-mode:both;animation-timeline:view();animation-range:' . $e_range . '}'; 497 534 foreach ( self::fx_props( 'enter', '--tf-e' ) as $f => $d ) { 498 $scroll[] = ".on-enter--$f,.on-enter-all--$f>* {". $d . '}';535 $scroll[] = ".on-enter--$f,.on-enter-all--$f>*" . ( $f === $b_fx ? $b_sel : '' ) . '{' . $d . '}'; 499 536 } 500 537 // Stagger: each child's range starts a little later. … … 544 581 $scroll[] = '@keyframes tf-enter-now{from{' . $frame( '--tf-e' ) . '}}'; 545 582 $scroll[] = $now . "{animation-name:tf-enter-now;animation-fill-mode:both;animation-timeline:auto;animation-range:normal;animation-duration:$timed;animation-timing-function:$ease;animation-delay:$wait}"; 583 // Until the footer script has marked them (.tf-enter-wait from the head script), every entrance 584 // element holds its start frame, so a hero painted early does not show and then jump back. 585 $scroll[] = ':is(.tf-enter-wait ' . str_replace( ',', ',.tf-enter-wait ', $enter ) . "){animation-name:tf-enter-now;animation-fill-mode:both;animation-timeline:auto;animation-range:normal;animation-duration:$timed;animation-play-state:paused}"; 546 586 if ( $s['exit'] ) { 547 587 $scroll[] = $now . ':is(' . $exit . "){animation-name:tf-enter-now,tf-exit;animation-fill-mode:both,forwards;animation-timeline:auto,view();animation-range:normal,$x_range;animation-duration:$timed,auto;animation-timing-function:$ease,linear;animation-delay:$wait,0s}"; … … 628 668 } 629 669 670 $btn_tr = array(); // Transitions the default button effects need on buttons. 671 630 672 // ---- Hover ---- 631 673 if ( $s['hover'] ) { … … 649 691 $css[] = '.on-hover--fade:hover{opacity:var(--hover-fade-opacity, .8)}'; 650 692 $css[] = '.on-hover--brighten:hover{filter:brightness(var(--hover-brighten-amount, 1.1))}'; 693 // Default button hover: buttons join that effect's :hover rule (they keep their own transition). 694 $f = self::btn_fx( $s, 'btn_hover_fx', self::HOVER_FX ); 695 if ( $f ) { 696 $css = str_replace( ".on-hover--$f:hover{", ".on-hover--$f:hover," . self::btn_sel() . ':hover{', $css ); 697 $t = ' var(--hover-duration, .3s) var(--hover-timing, ease-in-out)'; 698 foreach ( array( 'scale', 'translate', 'box-shadow', 'opacity', 'filter' ) as $p ) { 699 $btn_tr[] = $p . $t; 700 } 701 } 651 702 } 652 703 … … 663 714 $ow = 'var(--border-outline-width, 4px)'; 664 715 $ot = 'var(--border-outline-duration, .3s) var(--hover-timing, ease-in-out)'; 665 $css[] = '.on-hover--outline-out,.on-hover--outline-in{outline:' . $ow . ' solid transparent;transition:outline-color ' . $ot . ',outline-offset ' . $ot . '}'; 716 $css[] = '.on-hover--outline-out,.on-hover--outline-in{outline:' . $ow . ' solid transparent}'; 717 // Written as attribute selectors so a default button effect never joins it (buttons get it through --btn-transition). 718 $css[] = '[class~="on-hover--outline-out"],[class~="on-hover--outline-in"]{transition:outline-color ' . $ot . ',outline-offset ' . $ot . '}'; 666 719 $css[] = '.on-hover--outline-out{outline-offset:0}.on-hover--outline-in{outline-offset:calc(' . $ow . ' * 2)}'; 667 720 $css[] = '.on-hover--outline-out:hover{outline-color:var(--border-outline-color, currentColor);outline-offset:' . $ow . '}'; … … 669 722 // Underline drawn by a pseudo-element, growing from the chosen side. 670 723 $u = '.on-hover--underline-left,.on-hover--underline-center,.on-hover--underline-right'; 671 $css[] = ':where( ' . $u . '){position:relative}';724 $css[] = ':where([class~="on-hover--underline-left"],[class~="on-hover--underline-center"],[class~="on-hover--underline-right"]){position:relative}'; // Buttons: below, without the focus exception. 672 725 $css[] = ':is(' . $u . ')::after{content:"";position:absolute;inset-inline:0;inset-block-end:0;block-size:var(--border-underline-width, 3px);background:var(--border-underline-color, currentColor);scale:0 1;pointer-events:none;transition:scale var(--border-underline-duration, .3s) var(--hover-timing, ease-in-out)}'; 673 726 $css[] = '.on-hover--underline-left::after{transform-origin:left}.on-hover--underline-center::after{transform-origin:center}.on-hover--underline-right::after{transform-origin:right}'; … … 676 729 $classes[] = "on-hover--$f"; 677 730 } 731 // Default button border effect: buttons join every selector list of that effect's class. 732 $f = self::btn_fx( $s, 'btn_border_fx', self::BORDER_FX ); 733 if ( $f ) { 734 // The pseudo part (:hover, ::after) is repeated for the button; a keyboard focus ring wins over the effect. 735 $btn = ':where(' . self::btn_sel() . ':not(:focus-visible))'; 736 $css = preg_replace( '/\.on-hover--' . $f . '((?:::?[a-z-]+)*)(?=[,{)])/', '$0,' . $btn . '$1', $css ); 737 if ( 0 === strpos( $f, 'underline-' ) ) { 738 $css[] = self::btn_sel() . '{position:relative}'; 739 } elseif ( 0 === strpos( $f, 'outline-' ) ) { 740 $btn_tr[] = 'outline-color ' . $ot; 741 $btn_tr[] = 'outline-offset ' . $ot; 742 } 743 } 744 } 745 // Buttons animate the default effects: their transition comes from --btn-transition (Components), extended here. 746 // A custom one (Buttons screen) is kept; "none" or a CSS-wide keyword stays as set (a list after it is invalid), "all" already covers the effect. 747 $bt = self::clean( Components::settings()['btn_transition'] ?? '' ); 748 if ( $btn_tr && ! preg_match( '/^(none|inherit|initial|unset|revert(-layer)?)$/i', $bt ) && ! preg_match( '/(?:^|,)\s*all(?![\w-])/i', $bt ) ) { 749 $css[] = self::btn_sel() . '{--btn-transition:' . ( '' === $bt ? 'var(--transition, background-color .15s, color .15s, border-color .15s)' : $bt ) . ',' . implode( ',', $btn_tr ) . '}'; 678 750 } 679 751 … … 702 774 $sel = array(); 703 775 if ( $s['enter'] ) { 704 $sel[] = self::sel( 'enter', self::SCROLL_FX ) . ',.on-enter--parallax' ;776 $sel[] = self::sel( 'enter', self::SCROLL_FX ) . ',.on-enter--parallax' . ( self::btn_fx( $s, 'btn_enter_fx', self::SCROLL_FX ) ? ',' . self::btn_sel() : '' ); 705 777 } 706 778 if ( $s['exit'] ) { … … 736 808 } 737 809 if ( $s['enter'] ) { 738 $add[] = 'if( !(W.CSS&&CSS.supports&&CSS.supports("animation-timeline: view()"))){R.classList.add("tf-motion-fallback")}';810 $add[] = 'if(W.CSS&&CSS.supports&&CSS.supports("animation-timeline: view()")){R.classList.add("tf-enter-wait")}else{R.classList.add("tf-motion-fallback")}'; 739 811 } 740 812 if ( ! $add ) { … … 744 816 (function(){var R=document.documentElement,W=window;if(!("IntersectionObserver" in W)||(W.matchMedia&&W.matchMedia("(prefers-reduced-motion: reduce)").matches)){return} 745 817 %ADD% 746 setTimeout(function(){if(!W.tfMotionReady){R.classList.remove("tf-motion","tf-motion-fallback" )}},4000)})();818 setTimeout(function(){if(!W.tfMotionReady){R.classList.remove("tf-motion","tf-motion-fallback","tf-enter-wait")}},4000)})(); 747 819 JS; 748 820 return str_replace( '%ADD%', implode( '', $add ), $js ); … … 756 828 */ 757 829 public static function script() { 758 // shortcut: a hero painted before this footer script runs shows once, then plays from the start (.tf-now); hide entrance elements from the head script if that flash shows on real sites.759 830 $s = self::settings(); 760 831 $t = (float) $s['visible_threshold']; … … 762 833 $js = <<<'JS' 763 834 (function(){var d=document,R=d.documentElement,W=window;W.tfMotionReady=1; 764 d.querySelectorAll(".on-enter--stagger,.on-exit--stagger,.on-visible--stagger").forEach(function(p){for(var i=0;i<p.children.length;i++){p.children[i].style.setProperty("--tf-i",i)}}); 765 if(W.CSS&&CSS.supports&&CSS.supports("animation-timeline: view()")){var tn=[];d.querySelectorAll('[class*="on-enter"]').forEach(function(e){var c=e.getAttribute("class")||"";if(!has(c,"enter","stagger|parallax")||/(^|\s)on-enter--parallax(\s|$)/.test(c)){return}[].forEach.call(/(^|\s)on-enter-all--/.test(c)?e.children:[e],function(x){var r=x.getBoundingClientRect();if(r.top<W.innerHeight&&r.bottom>0&&r.left<W.innerWidth&&r.right>0){tn.push(x)}})});tn.forEach(function(x){x.classList.add("tf-now")})} 835 if(W.CSS&&CSS.supports&&CSS.supports("animation-timeline: view()")){var tn=[];d.querySelectorAll('[class*="on-enter"]').forEach(function(e){var c=e.getAttribute("class")||"";if(!has(c,"enter","stagger|parallax")||/(^|\s)on-enter--parallax(\s|$)/.test(c)){return}[].forEach.call(/(^|\s)on-enter-all--/.test(c)?e.children:[e],function(x){var r=x.getBoundingClientRect();if(r.top<W.innerHeight&&r.bottom>0&&r.left<W.innerWidth&&r.right>0){tn.push(x)}})});tn.forEach(function(x){x.classList.add("tf-now")});R.classList.remove("tf-enter-wait")} 766 836 var RV='[data-tf-reveal="fade"],[data-tf-reveal="up"]',V=R.classList.contains("tf-motion"),F=R.classList.contains("tf-motion-fallback"),io; 767 837 var T=%T%,th=[0];for(var x=.05;x<T;x+=.05){th.push(x)}th.push(T); … … 770 840 function has(c,fam,skip){return new RegExp("(^|\\s)on-"+fam+"(-all)?--(?!("+skip+")(\\s|$))").test(c)} 771 841 function obs(e){io=io||new IntersectionObserver(function(es){es.forEach(function(e){if(e.isIntersecting&&(e.intersectionRatio>=T||e.intersectionRect.height>=W.innerHeight*T)){e.target.classList.add("tf-in");if(e.target.tfV){done(e.target)}io.unobserve(e.target)}})},{threshold:th});io.observe(e)} 772 function scan(r){var l=[].slice.call(r.querySelectorAll('[class*="on-visible"],[class*="on-enter"],[ data-tf-reveal]')),a=0,b=0;if(r.nodeType===1){l.unshift(r)}l.forEach(function(e){var c=e.getAttribute("class")||"";if(V&&!e.tfV&&(e.matches(RV)||has(c,"visible","stagger"))){e.tfV=1;a++;obs(e)}if(F&&!e.tfE&&has(c,"enter","stagger|parallax")){e.tfE=1;b++;obs(e)}});return[a,b]}773 var n=scan(d) ;774 if(!n[0]){V=0;R.classList.remove("tf-motion")}if(!n[1]){F=0;R.classList.remove("tf-motion-fallback")}842 function scan(r){var l=[].slice.call(r.querySelectorAll('[class*="on-visible"],[class*="on-enter"],[class*="on-exit"],[data-tf-reveal]')),a=0,b=0;if(r.nodeType===1){l.unshift(r)}l.forEach(function(e){var c=e.getAttribute("class")||"";if(/(^|\s)on-(enter|exit|visible)--stagger(\s|$)/.test(c)){for(var i=0;i<e.children.length;i++){e.children[i].style.setProperty("--tf-i",i)}}if(V&&!e.tfV&&(e.matches(RV)||has(c,"visible","stagger"))){e.tfV=1;a++;obs(e)}if(F&&!e.tfE&&has(c,"enter","stagger|parallax")){e.tfE=1;b++;obs(e)}});return[a,b]} 843 var n=scan(d),mo; 844 function cut(){if(!n[0]){V=0;R.classList.remove("tf-motion")}if(!n[1]){F=0;R.classList.remove("tf-motion-fallback")}if(!(V||F)&&mo){mo.disconnect()}} 775 845 if(!(V||F)){return} 776 new MutationObserver(function(ms){ms.forEach(function(m){[].forEach.call(m.addedNodes,function(x){if(1===x.nodeType){scan(x)}})})}).observe(d.body,{childList:true,subtree:true})})(); 846 mo=new MutationObserver(function(ms){ms.forEach(function(m){[].forEach.call(m.addedNodes,function(x){if(1===x.nodeType){var k=scan(x);n[0]+=k[0];n[1]+=k[1]}})})});mo.observe(d.body,{childList:true,subtree:true}); 847 if(d.readyState==="loading"){d.addEventListener("DOMContentLoaded",cut)}else{cut()}})(); 777 848 JS; 778 849 return str_replace( '%T%', (string) $t, $js );
Note:
See TracChangeset
for help on using the changeset viewer.