user-select
define se (e em que medida) os usuários podem selecionar o conteúdo de um elemento – o código a seguir mostra os vários valores válidos e explica as limitações que esses valores impõem. user-select
não tem nada a ver com ‘estilo’.
user-select: text; // text can be selected
user-select: none; // text can't be selected
user-select: contain; // within the element
user-select: all; // all or nothing
user-select: auto; // depends, see below…
<element>:before, <element>:after {
user-select: auto; // user-select: none;
}
<editable element> {
user-select: auto; // user-select: contain;
}
<any other element> {
user-select: auto;
// <child> inherits ‘all’ or ‘none’
// if no inheritance, then user-select: text;
}
Prefixando user-select
A prefixação é necessária para suporte máximo do navegador da web
-ms-user-select: <value>; // internet explorer
-moz-user-select: <value>; // firefox for android
-webkit-user-select: <value>; // opera, edge, safari
Usando user-select
Antes de encerrarmos, vamos explorar um user-select
caso de uso em que ‘travamos’ um artigo.
HTML
<article id="article">Lorem ipsum solor sit amet…</article>
JavaScript
if(articleShouldBeLocked) {
const article = document.querySelector("#article");
article.classList.add("locked"); // used for styling
article.querySelectorAll("a").forEach(a => a.setAttribute("tabindex", "-1")); // skip focusing
document.body.setAttribute("oncontextmenu", "return false;"); // block the right-click contextmenu
}
CSS
#article.locked {
opacity: 0.5; // styling
filter: blur(0.5rem); // styling
user-select: none; // block selections
pointer-events: none; // block link clicks
}
Perdi alguma coisa?
Bem, se eu fiz … então deixe um comentário já!