@@ -2,15 +2,15 @@ import { useContext, useMemo, useEffect } from 'preact/hooks';
22import { useSignalEffect } from '@preact/signals' ;
33import { deepSignal , peek } from 'deepsignal' ;
44import { directive } from './hooks' ;
5- import { prefetch , navigate , hasClientSideTransitions } from './router' ;
5+ import { prefetch , navigate , canDoClientSideNavigation } from './router' ;
66
77// Until useSignalEffects is fixed:
88// https://github.com/preactjs/signals/issues/228
99const raf = window . requestAnimationFrame ;
1010const tick = ( ) => new Promise ( ( r ) => raf ( ( ) => raf ( r ) ) ) ;
1111
12- // Check if current page has client-side transitions enabled .
13- const clientSideTransitions = hasClientSideTransitions ( document . head ) ;
12+ // Check if current page can do client-side navigation .
13+ const clientSideNavigation = canDoClientSideNavigation ( document . head ) ;
1414
1515const isObject = ( item ) =>
1616 item && typeof item === 'object' && ! Array . isArray ( item ) ;
@@ -91,19 +91,19 @@ export default () => {
9191 className : name ,
9292 context : contextValue ,
9393 } ) ;
94+ const currentClass = element . props . class || '' ;
95+ const classFinder = new RegExp (
96+ `(^|\\s)${ name } (\\s|$)` ,
97+ 'g'
98+ ) ;
9499 if ( ! result )
95- element . props . class = element . props . class
96- . replace (
97- new RegExp ( `(^|\\s)${ name } (\\s|$)` , 'g' ) ,
98- ' '
99- )
100+ element . props . class = currentClass
101+ . replace ( classFinder , ' ' )
100102 . trim ( ) ;
101- else if (
102- ! new RegExp ( `(^|\\s)${ name } (\\s|$)` ) . test (
103- element . props . class
104- )
105- )
106- element . props . class += ` ${ name } ` ;
103+ else if ( ! classFinder . test ( currentClass ) )
104+ element . props . class = currentClass
105+ ? `${ currentClass } ${ name } `
106+ : name ;
107107
108108 useEffect ( ( ) => {
109109 // This seems necessary because Preact doesn't change the class names
@@ -146,13 +146,13 @@ export default () => {
146146 } ) => {
147147 useEffect ( ( ) => {
148148 // Prefetch the page if it is in the directive options.
149- if ( clientSideTransitions && link ?. prefetch ) {
149+ if ( clientSideNavigation && link ?. prefetch ) {
150150 prefetch ( href ) ;
151151 }
152152 } ) ;
153153
154154 // Don't do anything if it's falsy.
155- if ( clientSideTransitions && link !== false ) {
155+ if ( clientSideNavigation && link !== false ) {
156156 element . props . onclick = async ( event ) => {
157157 event . preventDefault ( ) ;
158158
0 commit comments