-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Html block: Add option to allow preview by default #41066
base: trunk
Are you sure you want to change the base?
Conversation
@@ -42,6 +46,14 @@ export default function HTMLEdit( { attributes, setAttributes, isSelected } ) { | |||
]; | |||
}, [] ); | |||
|
|||
useEffect( () => { | |||
if ( !! showPreview ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @mauriac for working on this. I'm thinking do we really need if-else
condition? While doing review I observed both the variables isPreview
and showPreview
are in sync if we modify current statement something like this:
-const [ isPreview, setIsPreview ] = useState();
+const [ isPreview, setIsPreview ] = useState(false);
PS: Current default value of isPreview
variable is undefined
.
Looking forward to hearing from you
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @amustaque97 thank you for your review, but I am a bit lost;
I do not get the suggestion. Please do you mind enlighten me?
The sync between isPreview and showPreview is just a plus, but on second thought let's remove it.
Thank you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The sync between isPreview and showPreview is just a plus, but on second thought let's remove it.
What I mean to say is that we can remove if-else block and can write something like this
useEffect(() => {
setIsPreview( showPreview );
}, [showPreview])
Also, to change line here
-const [ isPreview, setIsPreview ] = useState();
+const [ isPreview, setIsPreview ] = useState(false);
I hope I'm able to explain request changes.
Thank you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mauriac , I saw your changes. I will take a proper look at it in sometime and update my comments here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The sync between isPreview and showPreview is just a plus, but on second thought let's remove it.
What I mean to say is that we can remove if-else block and can write something like this
useEffect(() => { setIsPreview( showPreview ); }, [showPreview])Also, to change line here
-const [ isPreview, setIsPreview ] = useState(); +const [ isPreview, setIsPreview ] = useState(false);I hope I'm able to explain request changes.
Thank you.
Oh yes you are totally right. I got it now, I make some changes, give me your back when you have sometime. thank you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your changes makes more sense to load preview setting once. Also, can we change useEffect
block to look like this:
useEffect( () => {
if ( showPreview ) {
switchToPreview();
}
}, [] );
I think PR is good to approve after the above final changes. Let me know what you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your changes makes more sense to load preview setting once. Also, can we change
useEffect
block to look like this:useEffect( () => { if ( showPreview ) { switchToPreview(); } }, [] );I think PR is good to approve after the above final changes. Let me know what you think?
I agree, I applied changes, so what about this change
+const [ isPreview, setIsPreview ] = useState(false);
should we still apply it?
This PR has frontend change I would like to involve @jasmussen and other bunch of folks for design/UX review 🙂 |
@@ -15,12 +16,15 @@ import { | |||
Disabled, | |||
SandBox, | |||
ToolbarGroup, | |||
PanelBody, | |||
ToggleControl, | |||
} from '@wordpress/components'; | |||
import { useSelect } from '@wordpress/data'; | |||
|
|||
export default function HTMLEdit( { attributes, setAttributes, isSelected } ) { | |||
const [ isPreview, setIsPreview ] = useState(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const [ isPreview, setIsPreview ] = useState(); | |
const [ isPreview, setIsPreview ] = useState( showPreview ); |
Suggestion: We can use attribute value as the initial state. It would be similar to the current effect that only runs on the mount.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this, @mauriac.
I left one suggestion, but the otherwise feature works as expected.
This shouldn't be an attribute of the block, we should probably look into making preview the default view when the block is unselected. |
Hello @mtias thanks for your comment. I think it makes sense, and I should agree with, but the advantage of the new option here is to allow users to choose what will be good for them, so if we remove the attribute, what happen for those who will not want to get the preview mode by default? Looking forward to hear you. Thanks |
+1 for this feature. And I don't think Also, Instead of using local state like below const [ isPreview, setIsPreview ] = useState( showPreview ); Call In ...
export default function HTMLEdit({ attributes, setAttributes, isSelected }) {
const { isPreview } = attributes;
...
function switchToPreview() {
setAttributes({ isPreview: true });
}
function switchToHTML() {
setAttributes({ isPreview: false });
}
return (
...
);
} *NOTE: Of course define ...
"attributes": {
...
"isPreview": {
"type": "boolean",
"default": false
}
},
... The user's toggled state is saved and remains even if the browser is reloaded. |
=> I think this will make editor UX bad (crackling? popping? <- sorry I forget English words to describe this phenomenon 😢 ) |
PR for on select approach: #46836 |
A new option has been added to HTML block to allow 'preview' mode by default
What?
fixes: #40913
Why?
See the issue.
How?
In this PR, a new attribute showPreview has been added to HTMLEdit component, and then when the state of this attribute changes the html view mode changes accordingly.
Testing Instructions
Screenshots or screencast