-
Notifications
You must be signed in to change notification settings - Fork 1
Lesson31-2: Added the ability to change the speed of the menu #51
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
Conversation
|
lesson31/src/css/style.css
Outdated
transition: transform 0.3s ease-in-out; | ||
will-change: transform; | ||
transition: transform ease-in-out; |
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.
CSS
- I removed
transition-duration
. -> JavaScript to determine and add. - Added
will-change
to make the animation smooth.
lesson31/src/js/drawer-menu.js
Outdated
const changeDirect = (ref, direct) => { | ||
const changeDirect = (menu, direct) => { | ||
switch(direct){ | ||
case "right": | ||
ref.classList.add("right"); | ||
menu.classList.add("right"); | ||
break; | ||
case "left": | ||
default: | ||
ref.classList.add("left"); | ||
menu.classList.add("left"); | ||
break; | ||
} | ||
} |
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.
Correction of previous PR
I have changed the argument name to menu
based on your previous comment.
ここのrefは簡単に書いてしまいましたが、関数名がMenuとして含まれているのでもっと具体的でいいと思います。前回書いていたものでも構わないです
#50 (comment)
break; | ||
} | ||
} | ||
|
||
const option = { direct: "left" }; //left or right | ||
const convertMillisecondsToSeconds = speed => `${speed / 1000}s`; |
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.
convert ms(ミリ秒) to s(秒)
- Since milliseconds were also used in libraries such as swiper and splide, milliseconds were adopted as an option.
- I created this function because we thought it would be more common to convert the number of seconds into the number of seconds when setting transition-duration.
lesson31/src/js/drawer-menu.js
Outdated
const changeSpeed = (menu, speed) => { | ||
const defaultSpeed = "0.4s"; | ||
const judgedSpeed = typeof speed === "number"? convertMillisecondsToSeconds(speed): defaultSpeed; | ||
menu.style.transitionDuration = judgedSpeed; | ||
} | ||
|
||
const option = { | ||
direct: "left", //left or right | ||
speed: 400 // number(ミリ秒) | ||
}; | ||
|
||
const initMenu = (menu, option = {}) => { | ||
changeDirect(menu, option?.direct); | ||
changeSpeed(menu, option?.speed); |
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.
Add option of speed
1. Add milliseconds to option.
2. In the changeSpeed function
- argument is typeof number -> run convertMillisecondsToSeconds()
- type of others -> set defaultSpeed(0.4s)
3. Execute the changeSpeed function in initMenu().
- const initMenu = (menu, option = {}) => {
-> If no options are available, {} will be passed
- changeSpeed(menu, option?.speed);
-> If there is no speed key in the options, undefined is passed
動作確認についてjs/drawer-menu.js 内のoption値を変更していただきたいです🙇♀️
① speedのミリ秒の数字を変更する-> ドロワーメニューの開閉スピードが変わる (header__navクラスにtransition-durationがセットされる) ② speedを"" や "abcdefg" など適当な文字に変更する-> errorがthrowされ、アニメーションがつかない。(開発者にエラーを伝える) ③ speedをコメントアウトする
-> デフォルト値0.4sの開閉スピードになる。 お手数をおかけいたしますが、よろしくお願いいたします🙇♀️ |
lesson31/src/js/drawer-menu.js
Outdated
|
||
const initMenu = (menu, option = {}) => { | ||
changeDirect(menu, option?.direct); | ||
changeSpeed(menu, option?.speed); |
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.
IMO
必ずしもchangeしないと思うのでset**でいい気がしますね
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.
ご指摘ありがとうございます!
その通りだと思ったので修正いたしました。
b4627ea
lesson31/src/js/drawer-menu.js
Outdated
changeDirect(ref, option?.direct); | ||
const changeSpeed = (menu, speed) => { | ||
const defaultSpeed = "0.4s"; | ||
const judgedSpeed = typeof speed === "number"? convertMillisecondsToSeconds(speed): defaultSpeed; |
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.
Q
speedをjudgeしているというよりはtypeをcheckしているのですよね?
あー、その後処理がありました。コメントしました
ref
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.
ご指摘ありがとうございます!
formatする関数と、setする関数を分けた方がわかりやすいと解釈しました!
一度修正してみたのですが、optionに数字以外のものが渡った場合は、どのようにすべきか迷ってしまいました。
b4627ea
【現状】
optionにspeedキーがないとき(引数にundefinedが渡る時)は、デフォルト引数の400が使用されます。
option = {speed: "a"}
などが誤って渡ったときは、デフォルト引数の値が使用されず、transition-durationがセットされない状態です。
【考えているコード】
下記のように実装するとundefinedにも数字以外の値にも対応できると考えたのですが、
(デフォルト引数は使用しなくなってしまいます..)
この書き方は 適していないでしょうか?
const formatSeconds = (speed) => {
const default = 400;
if(typeof speed !== "number"){
convertMillisecondsToSeconds(default);
return;
}
return convertMillisecondsToSeconds(speed);
}
オプション機能を使用する側にエラーを伝えてreturnする
(デフォルト値は渡さず、一旦アニメーションはつかなくなる)
ということも考えました。
const formatSeconds = (speed) => {
if(typeof speed !== "number"){
console.error("speedオプションには数字を入力してください");
return;
}
return convertMillisecondsToSeconds(speed);
}
ご教授いただけると嬉しいです。
お忙しい中恐れ入りますが、ご確認のほどよろしくお願いいたします。
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.
@kenmori
Discordでご教授いただきありがとうございました!
大変勉強になりました。
下記の場合に対応できるコードに修正しました。
- undefined -> デフォルト引数 speed = 400
- undefined, number以外 -> throw error
ご確認のほどよろしくお願いいたします🙇♀️
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 formatSeconds = (speed) => {
const default = 400;
if(typeof speed !== "number"){
convertMillisecondsToSeconds(default);
return;
}
return convertMillisecondsToSeconds(speed);
}
↑これが考えているコードですね
質問の内容は、speed
がundefined
で渡ってきた場合に対応するために上のように書いたがこれだとダメかということですね
いいのですが、
default
は必ずしもそこで定義しなくてもいいと思います
書くなら
const formatSeconds = (speed) => {
if(typeof speed !== "number"){
const default = 400;
convertMillisecondsToSeconds(default);
return;
}
return convertMillisecondsToSeconds(speed);
}
ただ、
speedがundefinedが渡ってくるとわかっているなら、
const formatSeconds = (speed = 400) => {
if(typeof speed !== "number"){
convertMillisecondsToSeconds(speed);
return;
}
return convertMillisecondsToSeconds(speed);
}
こうなりますね。これのいいところは不要な定義を避けているところです。
これを生かすとまだ冗長ですね
問題はnumber
かundefined
以外が渡ってくるとき、ですが
これは仕様的にあり得ないですが、もしそうならerror
にするか、変換するかなのですが、
全てのtype
に対応してチェックすると大変だと思うのでそこはどこまで堅牢にするかによるかと思います
全てのタイプをチェックしないのであれば、開発時にエラーをthrow
して気づかせることができます
const formatSeconds = (speed = 400) => {
if(typeof speed !== "number"){
throw new Error(`speed expect number type. but got ${typeof speed}`)
}
return convertMillisecondsToSeconds(speed);
}
個人的にはちゃんとやるなら上ですが、そこまで望まないです
ここを開発時にちゃんと保証させるのがTypeScriptです。JSで堅牢な関数を作るのであればちゃんと型チェックするのもいいですが、
console.error()
はあくまでクラッシュさせたくない場合に使います。ユーザーの画面で使うこともありますが、
エラーが出続けた状態で何か使われると不正なデータが入ったりしてさらにバグ発見が困難な状態になる場合は
クラッシュさせて伝えることも大事なので、そういう時はエラーを返します
lesson31/src/js/drawer-menu.js
Outdated
|
||
const initMenu = (ref, option = {}) => { | ||
changeDirect(ref, option?.direct); | ||
const changeSpeed = (menu, speed) => { |
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 changeSpeed = (menu, speed) => { | |
const formatSeconds = (menu, speed = 400) => { | |
return convertMillisecondsToSeconds(speed) | |
} |
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.
formatSeconds()のご提案コードありがとうございます!
上記と一緒に修正しております!
#51 (comment)
I specified the options as follows and this is how it works. Please check it.
image2.mov |
@nakagawamai I don't know if this is the right solution, but I decided to animate by changing the left or right value instead of tranform.
I would appreciate if you could confirm that it works and advise me on the code. |
@nakagawamai
consoleにerrorが表示されます。 |
@haru-programming
下記承知しました。
speedに文字列をいれると、ドロワーの動きに speedが影響しなくなるのは、確認できました!
すみません!console表示もできていました。問題ありません! |
Issue No.31
今回実装した仕様
=> スライドスピードのみ対応することとしました🙇♀️
実装済みの仕様
-> 左右どちらか選べるのみ実装
{direct: "left"} // or right
CodeSandBox
forkに失敗する場合のCodeSandbox(あらかじめfork ver.)